Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nubBy is not working as expected

Tags:

haskell

The function below should generate prime numbers however it does not for GHC 7.10.2. Is anybody else seeing this?

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> import Data.List
Prelude Data.List> print . take 100 . nubBy (\x y -> x `rem` y == 0) $ [2..]
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101]

The strange part is that it seems to work fine on this site:

rextester.com/LWZCQ71376

like image 559
Vanson Samuel Avatar asked Nov 04 '15 20:11

Vanson Samuel


1 Answers

What changed between base-4.7.x and base-4.8.0.0 is the definition of elem_by which is what nubBy is defined in terms of.

In base-4.7 elem_by has this clause:

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

and in base-4.8 it was changed to:

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

The history of this change is documented in these TRAC issues:

  • 2528
  • 3280
  • 7913

Note that the Haskell Report Prelude version of nubBy is:

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

which was at odds with the base-4.7 implementation, so that also explains the change.

like image 109
ErikR Avatar answered Nov 15 '22 09:11

ErikR