[Haskell-beginners] Creating beautiful code: can you make this divide-and-conquer implementation of the "tails" function beautiful?
Costello, Roger L.
costello at mitre.org
Mon Jun 27 22:30:47 CEST 2011
Below is a divide-and-conquer implementation of the "tails" function.
Notice the two patterns (x:y:xs) and (x:). And notice that (x:y:xs) is used by the "length" function and again by the "splitAt" function. That doesn't seem elegant. Can the function be simplified and made beautiful?
tails' :: [a] -> [[a]]
tails' (x:y:xs) = map (++zs) (tails' ys) ++ tails' zs
where m = length (x:y:xs)
n = m `div` 2
(ys,zs) = splitAt n (x:y:xs)
tails' (x:) = [[x]]
More information about the Beginners