
I am trying to relate the state monad to a stack example and somehow found it easy to get recursively confused! instance Monad (State s) where return x = State $ \s -> (x,s) (State h) >>= f = State $ \s -> let (a, newState) = h s (State g) = f a in g newState Considering the stack computation stackManip stack = let ((), newStack1) = push 3 stack (a, newStack2) = pop newStack1 in pop newStack2 in do notation this would become do push 3 a <- pop pop If I consider the first computation push 3 >>= pop and try to translate it to the definition there are problems.... Copy paste again, I have (State h) >>= f = State $ \s -> let (a, newState) = h s (State g) = f a in g newState f is the push function to which we are shoving the old state. I can't exactly get around to what exactly is the state computation h? Ok assuming it's something which gives me a new state, but then thats push which is function f. Then push is applied to a which is assuming 3 in this case. This gives me a new state, which I would say newStack1 from the stockManip above. Then somehow I apply g to newState?? All the more confusion. Back to the question what exactly is state computation and how is it different from f? It seems to be the same function? -Animesh