I saw that a similar question was asked before, but I don’t understand another issue here.
Here are two functions:
(define x 2)
(define a 2)
(define goo
(lambda (x)
(display x)
(lambda (y) (/ x y))))
(define foo
(let ((f (goo a)))
(lambda (x)
(if (= x 0)
x
(f x)))))
What is the return value of (foo (foo 0))? What will print out to the screen?
As I understand it, when I run (foo 0) in the beginning, 2 will print out (we will enter the function goo), and the return value will be 0. Then, we will enter the function foo again with (foo (foo 0)) => (foo 0). We again enter the function goo and 2 will print out. But when I run it, 2 is printed just once. I think I’m missing a critical issue about let and its connection to lambda.
The
letinside the definition offoo, and therefore the application ofgootoa, is evaluated whenfoois defined, not whenfoois evaluated.Look at it this way: what is the value of foo? It is the
lambdaexpression. The binding offis closed over byfoo, it is not “redone” every timefoois evaluated.Edit: here’s an example without
lambdasWhen you evaluate
baryou are not callingsqrtagain.baris defined as the body of thelet, in this case a number that is the result if an expression.In your example the body of the
letis alambdaexpression. But just like my example, theletbinding is not re-executed.