[Haskell] return?

Tomasz Zielonka t.zielonka at students.mimuw.edu.pl
Fri Apr 30 12:26:07 EDT 2004


On Thu, Apr 29, 2004 at 06:27:32PM -0500, Ben_Yu at asc.aon.com wrote:
> Hi,
> While writing monad programs, I sometimes want to do a return as it is in
> imperative program. i.e.,
> do{return 1; return 2} is same as return 1
> 
> Is this possible at all?

Someone already proposed an Error monad, but I think that Continuation
monad is more appropriate, as it's easier to ,,return'' values of
different types. It also allows you to choose the place you want to
,,return'' to.

You'll probably want to use features of other monad, so use a ContT
monad transformer.

Here's an example:

    import Control.Monad.Cont

    code = (\m -> runContT m return) $ do
	x <- callCC $ \exit -> do
	    exit 1
	    return 2
	lift (print x)

This will print 1, not 2.

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links


More information about the Haskell mailing list