[Haskell-cafe] groupBy huh?

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Mar 4 01:47:11 CET 2011


On Friday 04 March 2011 01:18:07, Jacek Generowicz wrote:
> Hi Cafe,
> 
> It seems that I don't understand what groupBy does.
> 
> I expect it to group together elements as long as adjacent ones
> satisfy the predicate, so I would expect ALL four of the following to
> give one group of 3 and a group of 1.
> 
> Prelude> :m + Data.List
> Prelude Data.List> groupBy (<) "abcb"
> ["abcb"]
> Prelude Data.List> groupBy (<) "abca"
> ["abc","a"]
> Prelude Data.List> groupBy (<) [1,2,3,2]
> [[1,2,3,2]]
> Prelude Data.List> groupBy (<) [1,2,3,1]
> [[1,2,3],[1]]
> 
> What am I missing?
> 

That groupBy expects an equivalence relation (iirc, that was documented 
some time, seems to be gone, there's only a hint left at the docs for group 
"equality test"). It tests subsequent elements against the first of the 
group, not adjacent elements.



More information about the Haskell-Cafe mailing list