Reputation:
I'm back with another scheme question. This is an example from Dybvig's book The Scheme Programming Language, and I understand what is happening:
(let ([x 'a])
(let ([f (lambda (y) (list x y))])
(f 'b))) --> (a b)
However he immediately follows it up with a modified example that I don't understand:
(let ([f (let ([x 'sam])
(lambda (y z) (list x y z)))])
(f 'i 'am)) --> (sam i am)
In the call (f 'i 'am)
I don't know what's going on, that's my confusion. I don't see how f is defined with two parameters. How do the lambda parameters bubble up, for lack of a better term, to f?? I would think that the code let ([x 'sam])
would block that from happening. But that isn't the case obviously. I hope my English phrasing of my confusion makes sense.
Upvotes: 2
Views: 40
Reputation: 6426
f
is defined as (lambda (y z) (list x y z))
, where x
is defined as 'sam
.
Let's break this down a bit.
(let ([x 'sam])
(lambda (y z) (list x y z)))
This evaluates to the lambda (which I'll call that_lambda
because I can).
(let ([f that_lambda])
(f 'i 'am)) --> (sam i am)
This evaluates to (that_lambda 'i 'am)
.
Because that_lambda
was defined* in the scope* where x
was 'sam
, x
used inside that_lambda
refers to 'sam
.
* It's so long since I've done Lisp that I don't know whether my terms are correct.
Upvotes: 2