On Mar 8, 2015, at 1:34 AM, Animesh Saxena <animeshsaxena@icloud.com> wrote:_______________________________________________Thanks for the links, I am able to get my head around this a bit.Here's my simple question1. In the stack example what is the state. As per my understanding the state is the new stack.If above statement is correct than by definition of >>=(>>=) :: State s a -> (a -> State s b) -> State s bpush 3 stackThis returns ((), newstack1)Now in this case the state or context is newstack1 which has to be passed to the next function if i apply >>=.push 3 stack >>= pop newstack1This might make sense coz I am shoving (or binding) the state to the pop function. State in this case is the stack which is being passed around or shoved around. Problem is pop doesn't need "a" argument like the definition of (>>=) indicates. It can very well produce a new state or return a new state.So if state = stack then this makes sense to me, where am i wrong...??push 3 stack >>= pop newstack1
On Mar 06, 2015, at 09:09 PM, "Sumit Sahrawat, Maths & Computing, IIT (BHU)" <sumit.sahrawat.apm13@iitbhu.ac.in> wrote:I won't comment on what state exactly is, but you can read up on that and gain some intuition here: https://en.wikibooks.org/wiki/Haskell/Understanding_monads/StateIt's helpful to implement it using a pen and paper, and consider how the state flows and gets transformed.According to the below example,stackManip stack =let ((), newStack1) = push 3 stack(a, newStack2) = pop newStack1in pop newStack2We get,push :: a -> Stack a -> ((), Stack a) -- Assuming 'Stack a' is a defined datatypepop :: Stack a -> (a, Stack a) -- Representing a stack with elements of type 'a'Thus,push 3 >>= pop~~ (Stack a -> ((), Stack a)) >>= (Stack a -> (a, Stack a)) { Replacing by types }Bind (>>=) has the type, (for "State s")(>>=) :: State s a -> (a -> State s b) -> State s bThis is a type mismatch. The conversion to do syntax is at fault here.First, you must write the computation using bind (>>=), and then convert to do-notation.On 7 March 2015 at 10:12, Animesh Saxena <animeshsaxena@icloud.com> wrote: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) wherereturn x = State $ \s -> (x,s)(State h) >>= f = State $ \s -> let (a, newState) = h s(State g) = f ain g newStateConsidering the stack computationstackManip stack = let((), newStack1) = push 3 stack(a, newStack2) = pop newStack1in pop newStack2in do notation this would becomedopush 3a <- poppopIf 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 ain g newStatef 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
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
--RegardsSumit Sahrawat_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners