import Control.Concurrent (forkIO) #if defined(STRICT) import Control.Concurrent.Chan.Strict #else import Control.Concurrent.Chan #endif import System.Environment -- Fork some computation processes, print their results main = do n <- getArgs >>= readIO . head f1 <- run fibonacci f2 <- run fibonacci2 mapM_ print . take n $ zip f1 f2 -- fork a process, return any messages it produces as a list where run f = do c <- newChan l <- getChanContents c forkIO (writeList2Chan c f) return l -- lazily returns values to the main thread to compute, should run twice -- as fast if we compute in the worker thread fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci) fibonacci2 = 1 : 1 : zipWith (+) fibonacci2 (tail fibonacci2)