Created
July 30, 2012 04:52
-
-
Save meetlai/3204798 to your computer and use it in GitHub Desktop.
Chinese Whisper
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
import Control.Monad | |
import Control.Concurrent | |
chans = sequence $ replicate 100001 newEmptyMVar | |
whisper chans = do | |
forM_ (zip chans (tail chans)) $ \(left,right) -> forkIO $ do | |
msg <- readMVar right | |
putMVar left (msg + 1) | |
forkIO $ putMVar (last chans) 1 | |
takeMVar $ head chans | |
main = chans >>= whisper >>= print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment