Skip to content

Unexpected behavior from Data.List.groupBy

I was hoping that

let notBoth1 a b = not (a == 1 && b == 1) in
groupBy notBoth1  [1,1,2,3,1,1,4,5,6,1]

would give me

[[1],[1,2,3,1],[1,4,5,6,1]]

but instead I get

[[1],[1,2,3],[1],[1,4,5,6],[1]]

It seems that groupBy assumes transitivity in the argument function. I have a new implementation that does not make this assumption. Of course, the implications of changing this function's behavior are troubling.

groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy' p (x : xs) = go [x] xs
    where
      go (x : xs) (y : zs) | p x y = go (y : x : xs) zs
      go g (y : zs) = reverse g : go [y] zs
      go g [] = [reverse g]
Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure IncorrectResultAtRuntime
Priority normal
Resolution Unresolved
Component Core Libraries
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information