Reputation: 83
recently I started learning scheme language
but I really don't know how to solve this problem.
(define A
(lambda()
(let* ((x 2)
(C (lambda (P)
(let ((x 4))
(P))))
(D (lambda ()
x))
(B (lambda ()
(let ((x 3))
(C D)))))
B)))
Q. What would be printed? How about shallow binding and deep binding?
I briefly understand define, lambda, let* but I have no idea to solve that problem. How can I solve it? please comment with detailed explanation
Upvotes: 0
Views: 402
Reputation: 18937
The procedure A returns the procedure B. So
(A)
returns
#<procedure:B>
If you want it to be executed, you have to run
((A))
which whill actually call B and return
2
So what is happening? Calling B will call C with D as a parameter. C will therefore call D (bound to parameter P). D returns x so what is printed is whatever value is bound to x in procedure D. Since Scheme is lexically scoped, and D does not have any binding for x, the binding of the lexical context is used, which comes from
(let* ((x 2)
which is why 2 gets printed, regardless of any other bindings to D that may exist in the other procedures.
Upvotes: 2