File: chan-test.hs

package info (click to toggle)
haskell-strict-concurrency 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 84 kB
  • sloc: haskell: 230; sh: 28; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 710 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

--
-- very computationally expensive jobs:

fibonacci = map fib [0..]

fibonacci2 = map fib [1..] -- to defeat CSE

fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)