
16 Jun
2011
16 Jun
'11
10:36 a.m.
This foldr version uses a partitioned accumulator and a post-processing step - it should be possible to achieve the same with an accumulator of [[a]] and deeper pattern matching: groupBy :: (a -> a -> Bool) -> [a] -> [[a]] groupBy test = post . foldr fn ([],[]) where post ([],yss) = yss post (xs,yss) = xs : yss fn a ([],yss) = ([a], yss) fn a (b:bs, yss) | test a b = (a:b:bs, yss) | otherwise = ([a], (b:bs):yss)