module sieve // The standard Sieve of Eratosthenes. //import StdEnv (+) infixl 6 :: !Int !Int -> Int (+) a b = code inline { addI } (-) infixl 6 :: !Int !Int -> Int (-) a b = code inline { subI } (==) infix 4 :: !Int !Int -> Bool (==) a b = code inline { eqI } (rem) infix 7 :: !Int !Int -> Int (rem) a b = code inline { remI } fro :: !Int -> [Int] fro i = [i:fro (i+1)] take :: Int [a] -> [a] take 0 _ = [] take n [x:xs] = [x:take (n-1) xs] NrOfPrimes :== 1000 // The sieve algorithm: generate an infinite list of all primes. Start = take NrOfPrimes (sieve (fro 2)) where sieve [prime:rest] = [prime : sieve (filter prime rest)] filter p [h:tl] | h rem p == 0 = filter p tl = [h : filter p tl] filter p [] = []