[Haskell-cafe] Cont, ContT and IO()
inforichland at gmail.com
Fri Jul 3 19:36:27 EDT 2009
Well, continuations come from Scheme, and by and large, they are usually
used in languages like Scheme (i.e. PLT web server), or Smalltalk (Seaside
web server), but they can be very useful in e.g. cases like yours for making
a convenient way to make a local exit. I did this in one toy game program
of mine. The code looks (somewhat) like this:
run :: GameState ()
run = (`runContT` id) $ do
throwaway <- callCC $ \exit -> forever $ do
-- retrieve the current state
-- get user input, etc...
case input of
"quit" -> exit $ return ()
in this case, when the user enters "quit" the captured continuation is
restored and the value '()' is returned from callCC and assigned to
'throwAway' in this case.
Of course, this is only one use case of continuations, a very powerful
abstraction mechanism :)
2009/7/3 Günther Schmidt <gue.schmidt at web.de>
> I've got an IO action, some file system IO, traversing one level only and
> iterating over files found. I wish to build in an "early" exit, ie. if an IO
> action in the loop encounters a particular value I want it to abort the
> Now so far, pls don't shoot, I have done this by throwing IO Exceptions and
> catching them. I'm trying to rewrite this using Continuatios / callCC but
> can't figure out where to place what.
> I certainly don't have the intuition yet and funny enough not even in RWH I
> could find some Cont/ContT examples.
> Would someone please draw me an example?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe