Skip to content

Instantly share code, notes, and snippets.

@olligobber
Created September 15, 2023 23:10
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 olligobber/818bd27243554a583afcf0db5c251bdc to your computer and use it in GitHub Desktop.
Save olligobber/818bd27243554a583afcf0db5c251bdc to your computer and use it in GitHub Desktop.
Bonding (xkcd.com/1188) for haskell
{-# LANGUAGE RecursiveDo #-}
import Control.Concurrent (ThreadId, throwTo, yield, forkIO)
import Control.Exception (Exception, catch)
import Control.Monad (forever)
wait :: IO ()
wait = forever yield
data Ball = Ball deriving Show
instance Exception Ball
type Person = ThreadId
person :: Person -> IO Person
person target = forkIO $
forever $ wait `catch` (\Ball -> throwTo target Ball)
main :: IO ()
main = mdo
parent <- person child
child <- person parent
throwTo parent Ball
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment