Created
September 20, 2010 18:54
-
-
Save srikumarks/588430 to your computer and use it in GitHub Desktop.
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
module Main where | |
import Control.Concurrent.Chan | |
import Control.Concurrent | |
-- A sieve that receives numbers from "inChan", filters | |
-- out the factors of "prime" and sends the results | |
-- out through "outChan". | |
sieve inChan outChan prime = do | |
print prime | |
ns <- getChanContents inChan | |
writeList2Chan outChan [n | n <- ns, n `mod` prime /= 0] | |
main = do | |
ch <- newChan | |
-- Start piping all numbers through the sieve chain. | |
forkIO (writeList2Chan ch [2..]) | |
setupSieves ch | |
where | |
setupSieves ch = do | |
next <- newChan | |
p <- readChan ch | |
-- Create a new sieve for each prime number found. | |
forkIO (sieve ch next p) | |
setupSieves next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment