(: https://cstheory.stackexchange.com/ , , .)
( 11 . '). , .
:
-- map (*i) [i ..] == map (*i) [i, i+1 ..] == [i*i, i*i+i ..]
-- sieve 2 [] where sieve i [] = (i:) $
-- sieve (i + 1) [i*i, i*i+i ..] == 2 : sieve 3 [4, 6 ..]
primesA :: [Integer]
primesA = 2 : sieve 3 [4, 6 ..]
sieve p cs@(c : t)
| p == c = sieve (p + 1) t
| otherwise = p : sieve (p + 1) (unionI cs [p*p, p*p+p ..])
unionI a@(x:xs) b@(y:ys) | x < y = x : xs `unionI` b
| x == y = x : xs `unionI` ys
| otherwise = y : a `unionI` ys
. (...(((a+b)+c)+d)+...), , .
. . 5, [25, 30 ..] . , 25. ( Θ(n) Θ(sqrt(n/log n)), n- ). .
, ( ps , , ):
primesAC = [2,3] ++ sieve 4 (tail primesAC) 9 [4,6..]
sieve k ps@(p:pt) q cs@(c:ct) -- candidate "k", prime "p"
| k == c = sieve (k + 1) ps q ct -- square "q", composite "c"
| k < q = k : sieve (k + 1) ps q cs
| otherwise = sieve k pt (head pt^2) -- k == q == p*p
(unionI cs [q, q+p ..])
Richard Bird, foldr, a+(b+(c+(...))), , :
primesAD = (2 :) . sieve 3 .
foldr (\p r-> p*p : unionI [p*p+p, p*p+2*p ..] r) [] $ primesAD
sieve p cs@(c : t) -- == diffI [p..] cs, if p<=c
| p == c = sieve (p + 1) t
| otherwise = p : sieve (p + 1) cs
, , .
So foldr, foldl, .
primesB :: [Integer]
primesB = [2..] `diffI` composites
composites = foldl f [4,6..] [3..]
where
f cs@(h:t) i | i == h = cs
| otherwise = h : unionI t [i*i, i*i+i ..]
diffI (x:xs) (y:ys) | x < y = x : xs `diffI` (y:ys)
| x == y = xs `diffI` ys
| otherwise = (x:xs) `diffI` ys
, , composites , ; , foldl , ( ). foldl .:) iterate, . , , :
composites = foldl f [4,6..] [3..]
-- no part of calculation of (f [4,6..] 3) is forced here, but if it were, ...
= foldl f (f [4,6..] 3) [4..]
= foldl f (4:unionI [6,8..] [9,12..]) [4..]
= foldl f (4:unionI [6,8..] [9,12..]) [5..]
= foldl f (4:union (unionI [6,8..] [9,12..]) [25,30..]) [6..]
= foldl f (4:union (union (unionI [6,8..] [9,12..]) [25,30..]) [36,42..]) [7..]
= ....
([2..] `diffI`) sieve 2 , .