[Haskell-cafe] callCC inside expression

Danny Gratzer danny.gratzer at gmail.com
Thu Jul 24 19:08:43 UTC 2014

> Is this correct and why is that so? Are scheme's call/cc and haskell's callCC semantically different?

Yes, Haskell's `callCC` is a user defined function and Scheme's is a
magical primitive. Under the hood Scheme should behave as if *every*
expression lived inside the continuation monad.

The Haskell equivalent would be something morally equivalent to

    foo :: Cont r Int
    foo = callCC $ \cont -> (+) <$> cont 1 <*> pure 1

Also, to do a lot of the fun games we can play with Scheme's call/cc, we
need ContT r IO vs plain old Cont.

For example, this program works "as expected" and returns 1

foo :: ContT r IO Int
foo = do
  ref <- lift (newIORef undefined)
  result <- callCC $ \c -> lift (writeIORef ref c) >> pure False
  if not result
    then lift (readIORef ref) >>= ($ True) >> return 0
    else return 1

More information about the Haskell-Cafe mailing list