[Haskell-cafe] Data.List.Split

Adam Vogt vogt.adam at gmail.com
Sun Dec 14 00:35:02 EST 2008


* On Saturday, December 13 2008, Gianfranco Alongi wrote:

>I have actually been thinking about a similar thing, but on the "group" subject.
>One can actually group things in many ways, such as groupBy (==) , so
>that groupBy (==) [1,2,1,2] should give
>[[1,1],[2,2]]. Of course other ideas are possible.

That result happens with:

> sortedGroups = group . sort

That composition is pretty, unlike those splitting functions. I don't know 
if manually fusing sort and group helps performance at all though.

Making groups by comparing with the previous element, or the first element 
of the current group is occasionally useful: (this does the former)

> groupIncDiff :: (a -> a -> Bool) -> [a] -> [[a]]
> groupIncDiff p = uncurry (:) . foldr w ([],[])
>     where w n ([],a) = ([n],a)
>           w n ((x:xs),a) | p x n = (n:x:xs,a)
>                          | otherwise = ([n],(x:xs):a)

(in case somebody feels like assembling the apparently numerous was to 
group)

On another note, is there much use of such simple library functions: does 
concatMap, for instance, save anything other than a couple parantheses, or 
does (concat . map) not necessarily get optimized into the same thing?

Adam


More information about the Haskell-Cafe mailing list