Data.List.groupBy with non-transitive equality predicate

Henning Thielemann lemming at henning-thielemann.de
Wed Aug 29 16:02:53 EDT 2007


I was used to use groupBy to split a list before every element that
satisfies a predicate. E.g. splitting before every capital

Prelude> groupBy (\_ c -> not $ Char.isUpper c) "Hello World"
["Hello ","World"]

I also wanted to use this for splitting after specific elements.
But this fails. I get

Prelude> groupBy (\c _ -> c /= ',') "1, 2, 3, 4"
["1, 2, 3, 4"]

but I wanted
["1,", " 2,", " 3,", " 4"]

 I assumed that 'groupBy' would compare adjacent elements, but it seems to
compare the leading element of each block with subsequent elements. Since
the documentation doesn't tell which elements are actually compared it
seems to assume that the comparison is commutative and transitive. Maybe
this point should be made clearer.
 Additionally I think that comparing neighbouring elements is a useful
behaviour and I suggest an according variant of 'groupBy' for Data.List.
Opinions?


More information about the Libraries mailing list