Skip to content

Instantly share code, notes, and snippets.

@eush77
Last active August 29, 2015 14:11
Show Gist options
  • Save eush77/7c537ca0211670dfb184 to your computer and use it in GitHub Desktop.
Save eush77/7c537ca0211670dfb184 to your computer and use it in GitHub Desktop.
Stream of prime numbers, this time in Haskell
-- SICP 3.5.2: https://sarabander.github.io/sicp/html/3_002e5.xhtml#g_t3_002e5_002e2
sieve :: Integral a => [a] -> [a]
sieve (n : ns) = n : sieve (filter (\x -> x `mod` n /= 0) ns)
primes :: [Integer]
primes = sieve [2..]
main :: IO ()
main = let ns = take 100 primes
in putStrLn $ concat [show n ++ ", " | n <- ns] ++ "..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment