simple state monad exercises? (besides labeling trees)

View: New views
5 Messages — Rating Filter:   Alert me  

simple state monad exercises? (besides labeling trees)

by tphyahoo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can someone give some simple common scenarios where the state monad is
useful, besides labeling trees?

References to puzzles like those in project Euler or similar would be nice.

Thanks!
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: simple state monad exercises? (besides labeling trees)

by Thomas ten Cate :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I used the State monad to implement a Brainfuck [1] interpreter a few
months ago. It stored the program counter, pointer and the memory of
the machine.

There might have been a different (better?) way, but as I was trying
to learn more about monads, it was an obvious choice.

Thomas

[1] http://www.muppetlabs.com/~breadbox/bf/

On Mon, Jul 6, 2009 at 18:54, Thomas Hartman<tphyahoo@...> wrote:

> Can someone give some simple common scenarios where the state monad is
> useful, besides labeling trees?
>
> References to puzzles like those in project Euler or similar would be nice.
>
> Thanks!
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@...
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: simple state monad exercises? (besides labeling trees)

by Matthias Görgens-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Can someone give some simple common scenarios where the state monad is
> useful, besides labeling trees?

Emulating the VM given in this years ICFP programming contest was also
a good application of the state monad.  Of course you interprate much
simpler language imperative languages, too.  (However that might feel
a bit forced as an exercise.)
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: simple state monad exercises? (besides labeling trees)

by LannyRipple :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Off the top of my head state is important when getting from A to B
depends on the path you took.  As such a common scenario I find
myself in all the time is not having a good CLI craps game.  (And
which I resolve by rewriting in every language I learn.)  Stake,
current bet, bets outstanding, point.  Lots of state.  Also user
interaction, varying output, error conditions, etc. depending on how
complex you want.

A much simpler problem is to model some large number of throws using
different play strategies.  Removes all the icky user interaction.

Alternately you can just abuse toy problems.

  import Control.Monad.State

  fac n = execState (facs n) 1

  facs n = do
    y <- get
    if n == 0
      then return y
      else do
        put (y*n)
        facs (n-1)

  Enjoy,
  -ljr

Thomas Hartman wrote:

> Can someone give some simple common scenarios where the state monad is
> useful, besides labeling trees?
>
> References to puzzles like those in project Euler or similar would be nice.
>
> Thanks!
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@...
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: simple state monad exercises? (besides labeling trees)

by John Meacham :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Jul 06, 2009 at 12:54:54PM -0400, Thomas Hartman wrote:
> Can someone give some simple common scenarios where the state monad is
> useful, besides labeling trees?

Implementing the Union-Find data structure[1] for unification based type
inference. As far as I know, no good alternative exists that has the
same very low computational complexity of the true stateful
union-find. Also, it's computational complexity has the ackermann's
function in it, which is just neat.

        John

[1] http://en.wikipedia.org/wiki/Disjoint-set_data_structure

--
John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe