Reputation: 115
I have been teaching scheme language and in my textbook I hit on the below expression. My question is why it is evaluating on number '10'? Can someone please explain it to me? Meaning what local environment are created and what bindings are created to symbols.
Also I know that every lambda expression can be rewritten using let
. Any idea how to do that?
I would appreciate any help.
(let ((x 10))
(define y (lambda (x) x))
(define x 5)
(+ x (y x)))
Upvotes: 2
Views: 227
Reputation: 48745
Your code in a more readable form:
(let ((x 10))
(define y (lambda (x) x))
(define x 5)
(+ x (y x)))
You can apply the substitution method:
(+ x (y x)) ; ==>
(+ 5 (y 5)) ; ==>
(+ 5 ((lambda (x) x) 5)) ; ==>
(+ 5 5) ; ==>
10
You got it the wrong way. Every let
can be rewritten as a lambda
which is immediately called:
(let ((a aexpr) (b bexpr))
body ...)
; ==
((lambda (a b) body ...) aexpr bexpr)
Upvotes: 5