私の特定の問題は、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次に、次のように使用してカウンターを削除する方が Haskell-y であると考えましたfoldl。しかし、これは効果的ではありません。
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 は、リストの先頭の値を取得する無駄な試みで、素数の倍数の無限に多くのリストを調べています。
しかし、なぜそれは具体的にそれを行うのですか?