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. let (x',seed') = x seed (Amy de Buitl?ir)
2. Re: let (x',seed') = x seed (Johan Tibell)
----------------------------------------------------------------------
Message: 1
Date: Thu, 29 Apr 2010 16:45:02 +0100
From: Amy de Buitl?ir <[email protected]>
Subject: [Haskell-beginners] let (x',seed') = x seed
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
I'm confused by the syntax this function definition I found in a tutorial:
bind :: (a â StdGen â (b,StdGen)) â (StdGen â (a,StdGen)) â (StdGen
â
(b,StdGen))
bind f x seed = let (x',seed') = x seed in f x' seed'
The part that confuses me is "let (x',seed') = x seed". The left side
is a tuple, and the right side is two separate values, so how does the
binding work? I gather from the context of the example (a tutorial on
monads) that the value of x is bound to x', and the value of seed is
bound to seed', so that "bind f" is a function that acts on a tuple
and produces a new tuple. Is that correct?
Thank you in advance to anyone who can de-confuse me.
------------------------------
Message: 2
Date: Thu, 29 Apr 2010 17:58:08 +0200
From: Johan Tibell <[email protected]>
Subject: Re: [Haskell-beginners] let (x',seed') = x seed
To: Amy de Buitl?ir <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi Amy,
On Thu, Apr 29, 2010 at 5:45 PM, Amy de Buitléir <[email protected]> wrote:
> I'm confused by the syntax this function definition I found in a tutorial:
>
> bind :: (a â StdGen â (b,StdGen)) â (StdGen â (a,StdGen)) â (StdGen
> â
> (b,StdGen))
> bind f x seed = let (x',seed') = x seed in f x' seed'
>
> The part that confuses me is "let (x',seed') = x seed". The left side
> is a tuple, and the right side is two separate values, so how does the
> binding work? I gather from the context of the example (a tutorial on
> monads) that the value of x is bound to x', and the value of seed is
> bound to seed', so that "bind f" is a function that acts on a tuple
> and produces a new tuple. Is that correct?
>
>
The names and parenthesis are a bit confusing. First, the type signature
bind :: (a â StdGen â (b,StdGen)) â (StdGen â (a,StdGen)) â
(StdGen â
(b,StdGen))
is equivalent to
bind :: (a â StdGen â (b,StdGen)) â (StdGen â (a,StdGen)) â
StdGen â
(b,StdGen)
as â associates to the right
So in
bind f x seed
* f is a function of type (a â StdGen â (b,StdGen)),
* x is a function of type (StdGen â (a,StdGen)),
* and seed is a value of type StdGen.
We can now see how let
(x',seed') = x seed
makes sense: x is applied to seed, producing a value of type (a,StdGen), a
tuple, which components are assigned to the variables x' and seed'. It would
perhaps have been clearer if x was called g or some other name that would
make it clear that it's not of the same type as x'.
Hope this helps.
-- Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100429/30121841/attachment-0001.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 22, Issue 45
*****************************************