[Haskell-beginners] How does this function append the log to the beginning of the list?

Kim-Ee Yeoh ky3 at atamo.com
Mon Dec 24 16:14:47 CET 2012


Correction, should be:

x = do
    result <- Nothing
    return result



-- Kim-Ee


On Mon, Dec 24, 2012 at 8:12 PM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:

> > What value will result have in the following?
> > result <- gcd' 2 (3 `mod` 2)
>
> First of all, your question suggests a mental model of assignables, which
> you want to avoid because it will just end up confusing you.
>
> Instead, you want to come to grips with true (lambda) variables. Read on!
>
> 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).
>
> 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.
>
> Ditto for an annulus, not just circle.
>
> 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.
>
> The bigger problem however, is that in general, abstracting over this
> particular example, result doesn't have the value you think it should have!
>
> Take for instance:
>
> x :: Maybe Int
> x = return Nothing
>
> That's (almost!) the desugar of
>
> x = do
>     result <- return Nothing
>     return result
>
> Now result has type Int, so let's ask: what's its Int-value?
>
> See what I mean?
>
> Ans: This "result" is actually a lambda variable, as is plain to see after
> desugaring. That's its value!
>
>
> -- Kim-Ee
>
>
> On Mon, Dec 24, 2012 at 6:47 PM, <jugree at lavabit.com> wrote:
>
>> Hello.
>>
>> Could you explain this example(0)? Could you show its step by step
>> execution?
>>
>> gcd' :: Int -> Int -> Writer (DiffList String) Int
>> gcd' a b
>>     | b == 0 = do
>>         tell (toDiffList ["Finished with " ++ show a])
>>         return a
>>     | otherwise = do
>>         result <- gcd' b (a `mod` b)
>>         tell (toDiffList [show a ++ " mod " ++ show b ++ " = " ++ show (a
>> `mod` b)])
>>         return result
>>
>> Why does the above append the log to the beginning of the list?
>>
>> What value will result have in the following?
>>
>> result <- gcd' 2 (3 `mod` 2)
>>
>> (0) http://learnyouahaskell.com/for-a-few-monads-more#writer
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121224/47637cb4/attachment-0001.htm>


More information about the Beginners mailing list