Skip to content

Instantly share code, notes, and snippets.

@nouse
Created June 22, 2011 16:40
Show Gist options
  • Save nouse/1040501 to your computer and use it in GitHub Desktop.
Save nouse/1040501 to your computer and use it in GitHub Desktop.
generate prime list with sieve and unfoldr
import System.Environment
import Data.List
prime :: [Int]
prime = unfoldr (\(x:xs) -> Just (x, sieve x xs)) [2..]
prime' :: Int -> [Int]
prime' limit = unfoldr con [2..limit] where
con [] = Nothing
con (x:xs) = Just (x, sieve x xs)
sieve :: Int -> [Int] -> [Int]
sieve n xs = filter (\x -> (x `mod` n) > 0) xs
main = do
[limit] <- map read `fmap` getArgs
putStr $ show $ takeWhile (< limit) prime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment