Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created September 20, 2010 18:54
Show Gist options
  • Save srikumarks/588430 to your computer and use it in GitHub Desktop.
Save srikumarks/588430 to your computer and use it in GitHub Desktop.
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