On Thu, Apr 29, 2010 at 5:45 PM, Amy de Buitléir
<amy@nualeargais.ie> 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