Reputation: 962
The following is in my class notes for Scheme:
(let ((x 2) (y 3))
(let ((x 7) (z (+ x y)))
(* z x)))
The answer yields 35. Can someone explain this to me? So on the 2nd line z(+x y) the x value seems to be 2 but after that (* z x) the x value is 7? Thanks a lot
Upvotes: 2
Views: 61
Reputation: 48745
Perhaps the easier way to explain this is by looking at let as syntax sugar for anonymous procedure calls.
(let ((x 2) (y 3))
(let ((x 7) (z (+ x y)))
(* z x)))
Is the same as:
((lambda (x y)
((lambda (x z)
(* z x)) ; first here is x 7
7
(+ x y))) ; this x is from the outer
2
3)
Upvotes: 2
Reputation: 2456
(let ((x 2) (y 3))
Here the 1st let
is still in charge until all values have been bound.
(let ((x 7) (z (+ x y)))
Here the 2nd let
is in charge.
(* z x)))
If you want (x 7)
to be used in (z (+ x y))
then try let*
TEST
(let ((x 2) (y 3))
(let ((x 7) (z (+ x y)))
(* z x)))
> 35
(let ((x 2) (y 3))
(let* ((x 7) (z (+ x y)))
(* z x)))
> 70
Hope it helps.
Upvotes: 4