エラトステネスの篩
https://gist.github.com/803680 にインスパイアされて遅延評価を使う版を書いた。
primes = 2: sieve 3 (multiples primes) sieve x yys@(y:ys) | x == y = sieve (x+1) (dropWhile (== y) ys) | otherwise = x: sieve (x+1) yys multiples (x:xs) = xx: msort [xx+x,xx+2*x..] (multiples xs) where xx = x * x msort xxs@(x:xs) yys@(y:ys) | x < y = x: msort xs yys | otherwise = y: msort xxs ys