Slide 24
Slide 24 text
π ππππππ‘π β· π β πΊππ [π]
π ππππππ‘π = ππππππ‘ β πππ π π’ππππ₯ππ β ππππππ₯ππ
segments :: [a] -> Set [a]
segments = concat . map suffixes . prefixes
def segments[A](as: List[A]): Set[List[A]] =
concat(prefixes(as).map(suffixes(_)))
ghci> segments [1,2,3,4]
[[],[1],[],[1,2],[2],[],[1,2,3],[2,3],[3],[],[1,2,3,4],[2,3,4],[3,4],[4],[]]
scala> segments(List(1,2,3,4))
val res0: List[List[Int]] = List(List(), List(1), List(), List(1,2), List(2), List(), List(1,2,3), List(2,3),
List(3), List(), List(1,2,3,4), List(2,3,4), List(3,4), List(4), List())
// NOTE β the concat provided by Scala appends a list to
// another, and is the replacement for deprecated union
def concat[A](as: List[List[A]]): List[A] = as.flatten
Duplicate empty lists!
Well, the Functional Quantum
Programming paper was a draft
after all.
See next slide for a fix.