[Haskell-cafe] Behavior of groupBy

Hans van Thiel hthiel.char at zonnet.nl
Sat Apr 28 09:49:44 EDT 2007

Hello All,

The standard function groupBy of List.hs doesn't work as I expect in
this case:

groupBy (\x y -> (last x) == (last y)) ["abc", "bd","cac"]

results in:


where I want: 

[["abc","cac"], ["bd"]]

Am I doing something wrong, or is this a bug? The function is defined
(in List.hs in the hugs Data library) as :

-- | The 'groupBy' function is the non-overloaded version of 'group'.
groupBy 		:: (a -> a -> Bool) -> [a] -> [[a]]
groupBy _  []		=  []
groupBy eq (x:xs)	=  (x:ys) : groupBy eq zs
                           where (ys,zs) = span (eq x) xs


Hans van Thiel

More information about the Haskell-Cafe mailing list