Skip to content

Instantly share code, notes, and snippets.

@meetlai
Created July 30, 2012 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meetlai/3204798 to your computer and use it in GitHub Desktop.
Save meetlai/3204798 to your computer and use it in GitHub Desktop.
Chinese Whisper
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