<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Ok I got the confusion clear, I was referring to the wrong definitions of push and pop<div class=""><br class=""></div><div class="">pop = Stack $ (\x:xs) -> (x,xs)</div><div class="">push a = State $ \xs -> ((), xs)</div><div class=""><br class=""></div><div class="">Now the do operation makes sense coz now I can relate all this to simple monads….</div><div class=""><br class=""></div><div class="">(>>=) :: Monad m => m a -> (a -> m b) -> m b</div><div class="">Where m = state s being shoved around!</div><div class=""><br class=""></div><div class="">-Animesh</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 8, 2015, at 1:34 AM, Animesh Saxena <<a href="mailto:animeshsaxena@icloud.com" class="">animeshsaxena@icloud.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div class="">Thanks for the links, I am able to get my head around this a bit. </div><div class=""><br class=""></div><div class="">Here's my simple question</div><div class="">1. In the stack example what is the state. As per my understanding the state is the new stack.</div><div class="">If above statement is correct than by definition of >>=</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="msg-quote"><div dir="ltr" class=""><div data-mce-style="font-size: 12.8000001907349px;" style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">  (>>=) :: State s a -> (a -> State s b) -> State s b</span></div></div></div></blockquote></div><div class=""><div class="msg-quote"><div dir="ltr" class=""><div data-mce-style="font-size: 12.8000001907349px;" style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class=""><br class=""></span></div></div></div></div><div class="">push 3 stack </div><div class="">This returns ((), newstack1)</div><div class="">Now in this case the state or context is newstack1 which has to be passed to the next function if i apply >>=.</div><div class="">push 3 stack >>= pop newstack1</div><div class="">This 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. </div><div class=""><br class=""></div><div class="">So if state = stack then this makes sense to me, where am i wrong...??</div><div class=""><br class=""></div><div class="">push 3 stack >>= pop newstack1</div><div class=""><br class="">On Mar 06, 2015, at 09:09 PM, "Sumit Sahrawat, Maths & Computing, IIT (BHU)" <<a href="mailto:sumit.sahrawat.apm13@iitbhu.ac.in" class="">sumit.sahrawat.apm13@iitbhu.ac.in</a>> wrote:<br class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="msg-quote"><div dir="ltr" class="">I won't comment on what state exactly is, but you can read up on that and gain some intuition here: <a href="https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State" data-mce-href="https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State" class="">https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State</a><div class="">It's helpful to implement it using a pen and paper, and consider how the state flows and gets transformed.</div><div class=""><br class=""></div><div class="">According to the below example,</div><div class=""><br class=""></div><div class=""><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">  stackManip stack =</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">    let</span><span style="font-family:monospace,monospace;font-size:12.8000001907349px" data-mce-style="font-family: monospace,monospace; font-size: 12.8000001907349px;" class=""> ((), newStack1) = push 3 stack</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">        (a, newStack2)  = pop newStack1</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">    in pop newStack2</span></div></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class=""><br class=""></span></div>We get,<div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class=""><br class=""></span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">  push :: a -> Stack a -> ((), Stack a)    -- Assuming 'Stack a' is a defined datatype</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">  pop  :: Stack a -> (a, Stack a)          -- Representing a stack with elements of type 'a'</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class=""><br class=""></span></div>Thus,<div class=""><br class=""><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">    push 3 >>= pop</span></div></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">~~  (Stack a -> ((), Stack a)) >>= (Stack a -> (a, Stack a))               { Replacing by types }</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class=""><br class=""></span></div>Bind (>>=) has the type, (for "State s")<div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><br class=""></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class="">  (>>=) :: State s a -> (a -> State s b) -> State s b</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;" class=""><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;" class=""><br class=""></span></div>This is a type mismatch. The conversion to do syntax is at fault here.<div class="">First, you must write the computation using bind (>>=), and then convert to do-notation.</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On 7 March 2015 at 10:12, Animesh Saxena <span dir="ltr" class=""><<a href="mailto:animeshsaxena@icloud.com" data-mce-href="mailto:animeshsaxena@icloud.com" class="">animeshsaxena@icloud.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex" data-mce-style="margin: 0 0 0 .8ex; border-left: 1px #ccc solid; padding-left: 1ex;"><div class=""><div class="">I am trying to relate the state monad to a stack example and somehow found it easy to get recursively confused!</div><div class=""><br class=""></div><div class="">instance Monad (State s) where</div><div class="">    return x = State $ \s -> (x,s)</div><div class="">    (State h) >>= f = State $ \s -> let (a, newState) = h s</div><div class="">                                                    (State g) = f a</div><div class="">                                                     in g newState</div><div class=""><br class=""></div><div class="">Considering the stack computation</div><div class=""><br class=""></div><div class="">stackManip stack = let </div><div class="">              ((), newStack1) = push 3 stack</div><div class="">              (a, newStack2) = pop newStack1</div><div class="">               in pop newStack2</div><div class=""><br class=""></div><div class="">in do notation this would become </div><div class="">do  </div><div class="">   push 3  </div><div class="">   a <- pop  </div><div class="">   pop</div><div class=""><br class=""></div><div class="">If I consider the first computation push 3 >>= pop and try to translate it to the definition there are problems....</div><div class="">Copy paste again, I have </div><div class=""><div class="">    (State h) >>= f = State $ \s -> let (a, newState) = h s</div><div class="">                                                    (State g) = f a</div><div class="">                                                     in g newState</div></div><div class=""><br class=""></div><div class="">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. </div><div class="">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.</div><div class=""><br class=""></div><div class="">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?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-Animesh</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">                           </div></div><br class="">_______________________________________________<br class=""> Beginners mailing list<br class=""> <a href="mailto:Beginners@haskell.org" data-mce-href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br class=""> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" data-mce-href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class=""> <br class=""></blockquote></div><br class=""><br class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="">Regards</div><div dir="ltr" class=""><div class=""><br class=""></div><div class="">Sumit Sahrawat</div></div></div></div></div></div></div></div><div class="_stretch"><span class="body-text-content">_______________________________________________<br class="">Beginners mailing list<br class=""><a href="mailto:Beginners@haskell.org" data-mce-href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br class=""><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" data-mce-href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class=""></span></div></div></blockquote></div></div>_______________________________________________<br class="">Beginners mailing list<br class=""><a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners<br class=""></div></blockquote></div><br class=""></div></body></html>