<p dir="ltr">I gave an expansion of the state monad for a different computation on Reddit some time ago. Perhaps it will be useful to you:</p>
<p dir="ltr"><a href="http://www.reddit.com/r/haskell/comments/25fnrj/nicta_course_help_with_state_exercise/">http://www.reddit.com/r/haskell/comments/25fnrj/nicta_course_help_with_state_exercise/</a></p>
<p dir="ltr">Best,<br>
Ben</p>
<br><div class="gmail_quote">On Sat, 7 Mar 2015 5:09 am Sumit Sahrawat, Maths & Computing, IIT (BHU) <<a href="mailto:sumit.sahrawat.apm13@iitbhu.ac.in">sumit.sahrawat.apm13@iitbhu.ac.in</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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" target="_blank">https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State</a><div>It's helpful to implement it using a pen and paper, and consider how the state flows and gets transformed.</div><div><br></div><div>According to the below example,</div></div><div dir="ltr"><div><br></div><div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> stackManip stack =</font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> let</font><span style="font-family:monospace,monospace;font-size:12.8000001907349px"> ((), newStack1) = push 3 stack</span></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> (a, newStack2) = pop newStack1</font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> in pop newStack2</font></div></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"><br></font></div></div><div dir="ltr">We get,<div style="font-size:12.8000001907349px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> push :: a -> Stack a -> ((), Stack a) -- Assuming 'Stack a' is a defined datatype</font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> pop :: Stack a -> (a, Stack a) -- Representing a stack with elements of type 'a'</font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"><br></font></div>Thus,<div><br><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> push 3 >>= pop</font></div></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace">~~ (Stack a -> ((), Stack a)) >>= (Stack a -> (a, Stack a)) { Replacing by types }</font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"><br></font></div>Bind (>>=) has the type, (for "State s")<div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"> (>>=) :: State s a -> (a -> State s b) -> State s b</font></div><div style="font-size:12.8000001907349px"><font face="monospace, monospace"><br></font></div>This is a type mismatch. The conversion to do syntax is at fault here.<div>First, you must write the computation using bind (>>=), and then convert to do-notation.</div></div><div class="gmail_extra"></div><div class="gmail_extra"><br><div class="gmail_quote">On 7 March 2015 at 10:12, Animesh Saxena <span dir="ltr"><<a href="mailto:animeshsaxena@icloud.com" target="_blank">animeshsaxena@icloud.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>I am trying to relate the state monad to a stack example and somehow found it easy to get recursively confused!</div><div><br></div><div>instance Monad (State s) where</div><div> return x = State $ \s -> (x,s)</div><div> (State h) >>= f = State $ \s -> let (a, newState) = h s</div><div> (State g) = f a</div><div> in g newState</div><div><br></div><div>Considering the stack computation</div><div><br></div><div>stackManip stack = let </div><div> ((), newStack1) = push 3 stack</div><div> (a, newStack2) = pop newStack1</div><div> in pop newStack2</div><div><br></div><div>in do notation this would become </div><div>do </div><div> push 3 </div><div> a <- pop </div><div> pop</div><div><br></div><div>If I consider the first computation push 3 >>= pop and try to translate it to the definition there are problems....</div><div>Copy paste again, I have </div><div><div> (State h) >>= f = State $ \s -> let (a, newState) = h s</div><div> (State g) = f a</div><div> in g newState</div></div><div><br></div><div>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>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><br></div><div>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><span><font color="#888888"><div><br></div><div><br></div><div>-Animesh</div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div></font></span></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div><div class="gmail_extra">-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>
______________________________<u></u>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-<u></u>bin/mailman/listinfo/beginners</a><br>
</blockquote></div>