Lightweight pure functional monad-free type-safe coroutines. > import Prelude hiding (read, return) > read cont (i, is) = cont i is > write o cont is = (x, (o, os)) where (x, os) = cont is > return x is = (x, ()) Unlikely as it sounds, that code was motivated by http://www.dpmms.cam.ac.uk/~martin/Research/Publications/2007/hp07.pdf > chat alice bob = (alice_value, bob_value) where > (alice_value, alice_output) = alice bob_output > (bob_value, bob_output ) = bob alice_output Example: Diffie-Hellman key exchange. See http://en.wikipedia.org/wiki/Diffie–Hellman_key_exchange > p = 23 > g = 5 > alice a = > write "Hello" $ > write (g^a `mod` p) $ > read $ \b -> > let s = b^a `mod` p in > write "Goodbye" $ > return s > bob b = > read $ \message1 -> > write (g^b `mod` p) $ > read $ \a -> > let s = a^b `mod` p in > read $ \message2 -> > return s > main = print $ chat (alice 6) (bob 15)