Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Last active February 6, 2016 07:08
Show Gist options
  • Save kevin-lee/a5918e22ef5ef4ad9ead to your computer and use it in GitHub Desktop.
Save kevin-lee/a5918e22ef5ef4ad9ead to your computer and use it in GitHub Desktop.
let primes :: [Integer]
primes = sieve [2..]
where sieve :: [Integer] -> [Integer]
sieve (x : xs) = x : sieve [n | n <- xs, n `mod` x /= 0]
-- It works even without parameter types specified yet it is always good to have the type information
-- as it tells the users of the function how to use it.
-- It can also help you implement the function.
-- primes without parameter types (Uncomment it if you want to try).
-- let primes = sieve [2..]
-- where sieve (x : xs) = x : sieve [n | n <- xs, n `mod` x /= 0]
take 10 primes
-- result:
-- [2,3,5,7,11,13,17,19,23,29]
takeWhile (< 100) $ filter (> 10) primes
-- result:
-- [11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment