[Haskell-cafe] State Monad

Sam sam.g at myrealbox.com
Wed Mar 2 21:40:54 EST 2005


Hello again,

in fact I wrote the following state monad:
--
newtype State state value = State (state -> (state, value))

instance Monad (State state) where
    return v = State $ \s -> (s, v)
    State f >>= k = State $ \s -> let (s0, v0) = f s
                                      State g  = k v0
                                  in g s0

push a = State $ \s -> (a:s, a)
prog= do {push 100 ; push 1}
execute (State program) = fst (program [])
--
but I never use value, so is there a way not to write it in the monad definition?

Sam.



Sam G. wrote:
> I need a Monad to represent an internal stack. I mean I've got a lot of functions which operates on lists and I would not like to pass the list as an argument everytime. 
> 
> Could you help me writing this monad? To start, I just need a + function which will return the sum of the 2 toppest elements of the stack.
> 
> Thanks in advance,
> Sam.
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 



More information about the Haskell-Cafe mailing list