[Haskell-cafe] Using a monad to decompose a function into functions

Marcin Kosiba marcin.kosiba at gmail.com
Fri Mar 13 12:38:11 EDT 2009


On Friday 13 March 2009, Cristiano Paris wrote:
> 2009/3/13 Marcin Kosiba <marcin.kosiba at gmail.com>:
> > ...
> > Threading the state is not the problem. Maybe this will help:
> > what I have now:
> >
> > fsm world state = case state of
> >  first ->
> >    do_stuff_one
> >    (move_up, succ state)
> >  second ->
> >    do_stuff_two
> >    (move_left, succ state)
> >  third ->
> >     do_stuff_three
> >     (move_right, first)
> >
> > what I'd want to have is to say:
> > fsm world state = do
> >  do_stuff_one
> >  yield move_up
> >  do_stuff_two
> >  yield move_left
> >  do_stuff_three
> >  yield move_right
> >  fsm world state
> >
> > and have it "translated" to:
> >
> > fsm world state =
> >  do_stuff_one
> >  (move_up, \world' state' ->
> >    do_stuff_two
> >      (move_left, \world'' state'' ->
> >         do_stuff_three
> >           (move_right, fsm world'' state'')
>
> Hi,
>
> I've not fully understood your exact problem but I think you might
> have a look to Continuations and Delimited Continuations.
>
> Both can help you solve the problem with implementing a yield
> statement. You can have a look at one of my (rather) old blog's posts
> about how to implement yield/send statements a-la-python:
>
> http://monadicheadaches.blogspot.com/2008/01/python-25s-iterators-in-haskel
>l-sort-of.html
>
> Notice that blogspot messed up with code blocks so indentation looks
> bad and some character is even missing.

Hi,
	I've already checked those out. I tried using your yield implementation and 
while it works, I couldn't get it to work with the state monad.
	So while:
data RecPair a b = Nil | RP (b, a -> RecPair a b)
yield x = Cont $ \k -> RP (x, k)

	got me half-way to my goal, I couldn't figure out how to make something like:

yield' = do
  state <- get
  state' <- yield state
  put state'

Thanks!
	Marcin Kosiba
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090313/bd881942/attachment.bin


More information about the Haskell-Cafe mailing list