Skip to content

Argument order not preserved by nubBy

Hello.

I recently wanted to know how the element 4 in [2,4] is ruled out by:

nubBy (\x y -> x `mod` y == 0) [2,4]

and discussing this on #haskell we discovered that the documentation or the code is buggy.

The numBy source states that we keep the call to eq with arguments in the same order as in the reference implementation, which, comparing the following two lines, is not true:

nubBy eq (x:xs)         =  x : nubBy eq (filter (\ y -> not (eq x y)) xs)

elem_by eq y (x:xs)     =  y `eq` x || elem_by eq y xs

Also this is easily proved by defining:

nubBy' eq [] = []
nubBy' eq (x:xs) = x : nubBy' eq (filter (\ y -> not (eq x y)) xs)

Then running:

nubBy (\x y -> x `mod` y == 0) [2,4]

which yields [2] because of eq y x and

nubBy' (\x y -> x `mod` y == 0) [2,4]

which yields [2,4] because of eq x y.

Trac metadata
Trac field Value
Version 7.6.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Prelude
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