; binding of variables in lambda expressions ; what is the result of evaluating (foo 2) in each case ; Case 1 (define y 1) ;; y is bound in the global scope ; foo is bound in the global scope, x is local, y is free (define foo (lambda (x) (+ x y))) (foo 2) ; Case 2 (define y 2) ;; change the binding of y (foo 2) ;; re-evaluate foo ; Case 3 (define y 3) ;; change the binding of y again (foo 2) ;; re-evaluate foo ; Case 4 (let ((y 4)) (foo y)) ;; let creates local bindings only. (foo 2) ; Case 5 (define foo (let ((y 4)) ;; y is now bound locally inside of foo (lambda (x) (+ x y)))) (foo 2) ;; in scheme, we often write "helper" functions that do some of the ;; work of a main function. Rather than populating the global namespace ;; with these helper functions, we can lexically nest them inside the ;; scope of the main function. For example, instead of ;; defining tfact as a global name to be called by the ;; function fact, we can used a NAMED LET and nest it inside ;; the definition of fact: (define tfact (lambda (n m) (if (= n 0) m (tfact (- n 1) (* n m))))) (define fact (lambda (n) (tfact n 1))) ;; using a named let (define fact (lambda (n) (let tfact ((n n) (m 1)) ;; tfact is local to fact (if (= n 0) m (tfact (- n 1) (* n m)))))) (fact 10)