Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. How does this function append the log to the beginning of
the list? ([email protected])
2. Re: Cartesian Product in Standard Haskell Libraries (Kim-Ee Yeoh)
3. Re: How does this function append the log to the beginning
of the list? (Kim-Ee Yeoh)
4. Re: How does this function append the log to the beginning
of the list? (Kim-Ee Yeoh)
----------------------------------------------------------------------
Message: 1
Date: Mon, 24 Dec 2012 06:47:34 -0500 (EST)
From: [email protected]
Subject: [Haskell-beginners] How does this function append the log to
the beginning of the list?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;charset=iso-8859-1
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
------------------------------
Message: 2
Date: Mon, 24 Dec 2012 19:08:51 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Cartesian Product in Standard Haskell
Libraries
To: Chadda? Fouch? <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<capy+zdtqfn10kr7mjuaoxqzvsu0tcga-dvgl6mvt5ddo43c...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
> the result is of type [()] but for a cartesian n-product, you would like
[[a]]
Right. So what we have here is a product over a count of 0 sets, which is
isomorphic to the function space that has the null set as domain. The
latter has exactly one element: the trivial function.
My apologies for misreading what OP wrote:
> This looks to me to be a violation of the rule that the Cartesian
> product of an empty list of lists is a list with one element in
> it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
On Mon, Dec 24, 2012 at 4:42 PM, Chadda? Fouch? <[email protected]>wrote:
> On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger <[email protected]> wrote:
> >
> > > sequence []
> > []
> > it :: [()]
> >
> > This looks to me to be a violation of the rule that the Cartesian
> > product of an empty list of lists is a list with one element in
> > it. It looks to be a violation because "[]" looks like a name
> > for an empty list. But we also have
> >
> > > length (sequence [])
> > 1
> > it :: Int
> >
> > which almost reassures me.
> >
>
> Well the type of the first response is a dead give-away : the result
> is of type [()] but for a cartesian n-product, you would like [[a]]
> (with a maybe instantiated to a concrete type) ...
> What's happening here is that sequence is not "the cartesian
> n-product" in general, it is only that in the list monad but in
> "sequence []" there's nothing to indicate that we're in the list
> monad, so GHC default to the IO monad and unit () so sequence has the
> type "[IO ()] -> IO [()]" and there's no IO action in the list
> parameter, so there's nothing in the result list.
>
> Try :
> > sequence [] :: [[Int]]
> and you should be reassured.
>
> --
> Jeda?
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121224/28981c7c/attachment-0001.htm>
------------------------------
Message: 3
Date: Mon, 24 Dec 2012 20:12:13 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] How does this function append the log
to the beginning of the list?
To: [email protected]
Cc: "[email protected]" <[email protected]>
Message-ID:
<capy+zdqhnlqt4t_hhorl9h4bvtx4e9+z0ou3kgxhtio4agy...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
> 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, <[email protected]> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121224/8f030463/attachment-0001.htm>
------------------------------
Message: 4
Date: Mon, 24 Dec 2012 22:14:47 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] How does this function append the log
to the beginning of the list?
To: [email protected]
Cc: "[email protected]" <[email protected]>
Message-ID:
<CAPY+ZdT6poo+C9pM-YCVqx-=facrffudscw7u6j9sejzffu...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Correction, should be:
x = do
result <- Nothing
return result
-- Kim-Ee
On Mon, Dec 24, 2012 at 8:12 PM, Kim-Ee Yeoh <[email protected]> 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, <[email protected]> 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
>> [email protected]
>> 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.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 54, Issue 39
*****************************************