[Haskell-cafe] State Monad - using the updated state

Kurt Hutchinson kelanslists at gmail.com
Thu Jan 8 08:27:24 EST 2009

Ryan gave some great advice about restructuring your program to do
what you want, but I wanted to give a small explanation of why that's

2009/1/7 Phil <pbeadling at mail2web.com>:
>  I want to be able to do:
> Get_a_random_number
> < a whole load of other stuff >
> Get the next number as defined by the updated state in the first call
> <some more stuff>
> Get another number, and so on.

The issue you're having is that you're trying to do the "other stuff"
in your 'main', but main isn't inside the State monad. The only State
computation you're calling from main is getRanq1, but you really need
another State computation that does "other stuff" and calls getRanq1
itself. That's what Ryan's first suggestion implements. You need all
your "other stuff" to be done inside the State monad so that it has
read/update access to the current random state. So all your main does
is run a State computation. That computation calls getRanq1 itself and
then "other stuff" in between calls to getRanq1.

Does that make sense?


More information about the Haskell-Cafe mailing list