
23 Dec
2010
23 Dec
'10
4:12 p.m.
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