[Shootout-list] message in Haskell

Einar Karttunen ekarttun@cs.helsinki.fi
Sun, 19 Dec 2004 15:38:06 +0200


Hello

Here is the new message test in Haskell.
Should be quite fast as usual ;)

import Control.Concurrent
import Control.Monad
import System

thread :: MVar Int -> MVar Int -> IO ()
thread inp out = takeMVar inp >>= putMVar out . (+1) >> thread inp out

spawn cur _ = do next <- newEmptyMVar
                 forkIO $ thread cur next
                 return next

main = do n <- getArgs >>= readIO.head
          s <- newEmptyMVar
          e <- foldM spawn s [1..3000]
          f <- newEmptyMVar 
          forkIO $ replicateM n (takeMVar e) >>= putMVar f . sum
          replicateM n (putMVar s 0)
          takeMVar f >>= print


- Einar Karttunen