[Haskell-beginners] how does the State monad actually work?

Erlend Hamberg ehamberg at gmail.com
Tue May 24 11:00:17 CEST 2011


On 24 May 2011 09:41, Sean Perry <shaleh at speakeasy.net> wrote:
> when invoked as
>
> print $ evalState foo 0
>
> I get "blah" as expected. When I switch to execState I get the value
> 1. This is what confuses me. Where is the State coming from if it is
> not passed into foo? What are get and put operating on?

If you write it like this

    print $ (execState foo) 0

and note that

    execState foo :: Int -> Int

it's easier to see that evalState foo returns a *function* that takes an
initial state and then operates on this.

Hope this makes it a bit clearer. :-)

-- 
Erlend Hamberg
ehamberg at gmail.com



More information about the Beginners mailing list