State Transformer

Jorge Adriano
Fri, 11 Jan 2002 14:18:15 +0000

> Your example uses two state variables: a Char and an Int, paired up as
> a tuple.  Below, I use one state variable of type (Char,Int) for that,
> as hinted by your first attempt at the translation.  foo and bar will
> each need to take a parameter --- the reference to the state variable,
> due to the reference business.
> testfunc = do
>            r <- newSTRef ('x',0)
>            foo r
>            bar r
>            (c,n) <- readSTRef r
>            return n

Yeap, I could do it like this myself :)
The whole problem is with passing the 'r' as a parameter, which is precisly 
what I'm trying to avoid. I think I already understood how to use this monad. 
Its pretty different from the monad state I was expecting, and as far as I 
can see, to be used in distinct situations. 


> foo r = do
>         (c,n) <- readSTRef r
>         writeSTRef r ('a', n+1)
> bar r = do
>         (c,n) <- readSTRef r
>         writeSTRef r (c,n+2)
> tryTestFunc = runST testfunc