> <span style="font-family:arial,sans-serif;font-size:13px">What value will result have in the following?</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> result <- gcd' 2 (3 `mod` 2)</span><br>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">First of all, your question suggests a mental model of assignables, which you want to avoid because it will just end up confusing you.</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Instead, you want to come to grips with true (lambda) variables. Read on!</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">The neat thing about FP in general is that you can keep DRY'er than in other languages. Why? Because you can take any piece of code, circle almost anywhere, and the fragment you've got is an honest-to-goodness expression that you can evaluate (modulo the free variables in that fragment). </span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Define a name at the top-level and you're good to go replacing all lookalikes with that name. This is especially true in Haskell because of non-strict semantics, a.k.a. full beta-reduction.</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Ditto for an annulus, not just circle.</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Back to your original question about the value of result. First of all the whole line isn't even an expression, being part of the sugaring over monadic syntax, which is likely the source of the confusion.</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">The bigger problem however, is that in general, abstracting over this particular example, result doesn't have the value you think it should have!</span><br>
</div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Take for instance:</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">x :: Maybe Int</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">x = return Nothing</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">That's (almost!) the desugar of</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">x = do</font></div>
<div><font face="arial, sans-serif"> result <- return Nothing</font></div><div><font face="arial, sans-serif"> return result</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">Now result has type Int, so let's ask: what's its Int-value?</font></div>
<div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">See what I mean?</font></div><div><br></div><div>Ans: This "result" is actually a lambda variable, as is plain to see after desugaring. That's its value!</div>
<div><br></div><div class="gmail_extra"><br clear="all"><div>-- Kim-Ee</div>
<br><br><div class="gmail_quote">On Mon, Dec 24, 2012 at 6:47 PM, <span dir="ltr"><<a href="mailto:jugree@lavabit.com" target="_blank">jugree@lavabit.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello.<br>
<br>
Could you explain this example(0)? Could you show its step by step<br>
execution?<br>
<br>
gcd' :: Int -> Int -> Writer (DiffList String) Int<br>
gcd' a b<br>
| b == 0 = do<br>
tell (toDiffList ["Finished with " ++ show a])<br>
return a<br>
| otherwise = do<br>
result <- gcd' b (a `mod` b)<br>
tell (toDiffList [show a ++ " mod " ++ show b ++ " = " ++ show (a<br>
`mod` b)])<br>
return result<br>
<br>
Why does the above append the log to the beginning of the list?<br>
<br>
What value will result have in the following?<br>
<br>
result <- gcd' 2 (3 `mod` 2)<br>
<br>
(0) <a href="http://learnyouahaskell.com/for-a-few-monads-more#writer" target="_blank">http://learnyouahaskell.com/for-a-few-monads-more#writer</a><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>