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

Cristiano Paris frodo at theshire.org
Fri Mar 13 11:50:21 EDT 2009


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-haskell-sort-of.html

Notice that blogspot messed up with code blocks so indentation looks
bad and some character is even missing.

Bye,

Cristiano


More information about the Haskell-Cafe mailing list