Created
June 22, 2011 16:40
-
-
Save nouse/1040501 to your computer and use it in GitHub Desktop.
generate prime list with sieve and unfoldr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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