foldl
阻止它终止或产生输出的具体问题是什么?
首先,我实现了素数筛。这不是最好的,但它就像 (例如) 一样工作得很好take 20 primesA
。
primesA :: [Integer]
primesA = sieve 2 []
sieve :: Integral a => a -> [a] -> [a]
sieve i [] = (i:) $ sieve (i + 1) $ map (*i) [i ..]
sieve i composites@(h : t)
| i == h = sieve (i + 1) t
| otherwise = (i:) $ sieve (i + 1) $ unionIncreasing composites $ map (*i) [i ..]
unionIncreasing :: Ord a => [a] -> [a] -> [a]
unionIncreasing [] b = b
unionIncreasing a [] = a
unionIncreasing a@(ah:at) b@(bh:bt) | ah < bh = ah : at `unionIncreasing` b
| ah == bh = ah : at `unionIncreasing` bt
| otherwise = bh : a `unionIncreasing` bt
i
然后我认为使用foldl
如下方式消除计数器会更Haskell-y 。但这并不有效。
primesB :: [Integer]
primesB = [2..] `differenceIncreasing` composites
composites :: [Integer]
composites = foldl f [] [2..]
where f [] i = map (*i) [i ..]
f knownComposites@(h:t) i | i == h = knownComposites
| otherwise = (h:) $ unionIncreasing t $ map (*i) [i ..]
differenceIncreasing :: Ord a => [a] -> [a] -> [a]
differenceIncreasing [] b = []
differenceIncreasing a [] = a
differenceIncreasing (x:xs) (y:ys) | x < y = x : xs `differenceIncreasing` (y:ys)
| x == y = xs `differenceIncreasing` ys
| otherwise = (x:xs) `differenceIncreasing` ys
当我运行(例如)时,它既不会终止也不会产生任何输出head primesB
。
据推测,ghci 正在查看无限多的素数倍数列表,试图获得列表头部的值是徒劳的。
但它为什么专门这样做呢?