[Haskell-cafe] A bug of groupBy implement

Stephen Tetley stephen.tetley at gmail.com
Mon Dec 7 12:41:47 EST 2009

Hi L.Guo

Brent has replied with the right answer.

The definition of groupBy is below - the span in the where clause only
compares with the first element of the current sub-list:

-- | 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


list1 = [7,3,5,9,6,8,3,5,4::Int]

Here are two more runs on your input with a wee bit less clutter. In
both cases there are 'spans' where the numbers increase and decrease -
this is because the operation is comparing the rest of the input
against the first element in the 'span' not consecutive elements.

*GroupBy Data.List> groupBy (<) list1

*GroupBy Data.List> groupBy (<=) list1

Best wishes


More information about the Haskell-Cafe mailing list