[Haskell-cafe] List of numbers to list of ranges
Stephen Tetley
stephen.tetley at gmail.com
Thu Dec 23 22:12:54 CET 2010
I'd go with direct recursion for this one - the pattern of consumption
and production that generates the answer doesn't seem to neatly match
any of the standard recursion combinators (map, unfold, fold,
mapAccum, ...) nor exotic ones (skipping streams c.f. the Stream
fusion paper, apomorphisms, ...).
Direct recursion might be prosaic, but it is pleasantly obvious:
ranges :: (Num a, Eq a) => [a] -> [(a,a)]
ranges [] = []
ranges (a:as) = step (a,a) as
where
step (i,j) [] = [(i,j)]
step (i,j) (x:xs) | j+1 == x = step (i,x) xs
step (i,j) (x:xs) = (i,j) : step (x,x) xs
More information about the Haskell-Cafe
mailing list