[Haskell-cafe] Using the ContT monads for early exits of IO ?

Tim Wawrzynczak inforichland at gmail.com
Thu Jun 10 11:49:38 EDT 2010


Günther,

This is definitely one way to do it.  If you want to be able to "quit" the
IO action in the middle of a lengthy computation, that is one way that I,
personally, find very straightfoward.  You can find an example of this in my
Advgame package on Hackage, which uses this method to quit running the main
action (although I could have used a conditional to determine whether to
continue running the main loop, but continuations are more fun :P).

Small example:

> foo = (`runContT` id) $ do
>    dummy <- callCC $ \exit -> forever $ do
>        line <- liftIO getLine
>        if line == "quit" then exit $ return () else ... -- do whatever
else here...
>

Cheers,
 - Tim

2010/6/10 Günther Schmidt <gue.schmidt at web.de>

> Hi everyone,
>
> I'm about to write a rather lengthy piece of IO code. Depending on the
> results of some of the IO actions I'd like the computation to stop right
> there and then.
>
> Now I know in general how to write this but I'm wondering if this is one of
> those occasions where I should make use of the Cont monad to make an early
> exit.
>
> Günther
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100610/83ba61ec/attachment.html


More information about the Haskell-Cafe mailing list