[Haskell-cafe] Behavior of groupBy
Stefan O'Rear
stefanor at cox.net
Sat Apr 28 09:45:04 EDT 2007
On Sat, Apr 28, 2007 at 03:49:44PM +0200, Hans van Thiel wrote:
> 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:
>
> [["abc"],["bd"],["cac"]]
>
> 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
You are doing something wrong. groupBy is specified to never reorder
elements. You probably want to use sortBy first.
Stefan
More information about the Haskell-Cafe
mailing list