nubBy没有按预期工作 [英] nubBy is not working as expected
问题描述
下面的函数应该生成素数,但它不适用于GHC 7.10.2。是否有其他人看到这一点?
GHCi,版本7.10.2:http://www.haskell.org/ghc/: ?寻求帮助
Prelude> import Data.List
Prelude Data.List>打印。拿100。 nubBy(\ xy - > 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]
奇怪的是,它似乎在这个网站上工作得很好:
在base-4.7 elem_by
中有这个子句:
elem_by eq y(x:xs)= y`eq` x || elem_by eq y xs
,并在base-4.8中更改为:
elem_by eq y(x:xs)= x`eq` y || elem_by eq y xs
这些更改的历史记录记录在以下TRAC问题中:
请注意, nubBy
的Haskell Report Prelude版本是:
nubBy eq(x:xs)= x:nubBy eq(filter(\ y - > not(eq xy))xs)
与base-4.7实现不一致,所以这也解释了变化。
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:
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:
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.
这篇关于nubBy没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!