CS 227 Let and Letrec terminology and uses * Problems The two main problems to be solved by let and letrec are preventing ____________________________ and _______________________________ * let The syntax of LET is as follows The value returned by (let ((y 1)) (let ((y (+ y 3)) (x (+ y 2))) (+ x y))) is _____. Why isn't it 10 or 4? _______________________________________. Suppose z is not defined yet. What happens to (+ (let ((z (* 3.14 2.73))) (f z z)) z) when it is executed? ____________________________________________________ Why is the value of the following (let ((x 4)) (let ((x (* x x))) (let ((x (* x x))) (+ x x)))) 512? _______________________________________________________ * Environments ** in general An *environment* can be thought of as a _______________ or _______________ that maps __________________ to ___________________. Draw a picture of an environment here: A *binding* is ________________________________, made by LET or LAMBDA. A variable in a LET or LAMBDA is *bound* by that LET or LAMBDA if __________________________________________________________________. Such variables are called *formal parameters*. A variable is *free* in an expression if it occurs _______________ __________________________________________________________________. The bound free variables of the expression ((LAMBDA (f y) (f a (f y z))) f 3) are ____________. The bound variables are __________________. ** global The *initial global environment* maps names like _________________________ but not keywords, like define and let, to their standard values The *user global environment* consists of the _____________________________ plus our own __________________________ made using the keyword ____________. ** local the result of the following code (define x 2) (define square (lambda (x) (* x x))) (square 3) is: __________________ and then the result of the expression, x, is ________________ Draw a line from the x's in (* x x) to the place that they refer. ** Nesting The result of the following code: (let ((+ cons)) (let ((z 3.14159)) (+ (- z 1) z))) is ___________________________________ Is a procedure name a variable? ________________ Draw a picture of the environment for ((lambda (f y) (f a (f y z))) + 3) * Scope of a binding The *scope of a binding* is the _____________ of the program in which ___________________________________________. ** Hole A *hole in the scope* is an area of the program for which ________________________________________________________. ** Lexically scoped (Statically scoped) A programming language is *lexically scoped* if the scope of a binding can be determined ___________________________. A language is *dynamically scoped* if the scope of a binding can (in general) only be determined _______________________________. ** Closures A procedure object is formed by ___________________ and is also called a ________________________. A closure remembers the procedure's parameters, it's code and ________________________________________. Consider the code... (DEFINE add2 (LET ((two 2)) (LAMBDA (x) (+ x two)))) Fill in the details of the following for the evaluation of the DEFINE above Env0: + ~~~> [proecdure|args|...|Env0] add2 ~~~> [procedure | (x) | (+ x two) |_________] (LET ((two 2)) (LAMBDA (x) (+ x two)))) ;; this happens when _________________ Env1 (parent Env0): two ~~~> [___] (LAMBDA (x) (+ x two)))) ;; this happens when _________________ Continuing from the above, fill in the details for the evaluation of (LET ((b 0.5)) (/ b (add2 b))) Env2 (parent _______): b ~~~> [_____] (/ b (add2 b)) ;; this is the _______________________ Env3 (parent ________): x ~~~> [_______] (+ x two) ;; this is the body of _______________ In the evaluation of (+ x two), the value of the variable two is found by looking in Env3, and then its parent environment: ______________. That environment is the parent of Env3 because _________________. What would be different if we had defined add2 as follows? (DEFINE add2 (LAMBDA (x) (LET ((two 2)) (+ x two)))) ** letrec You need to use LETREC instead of LET to define _______________________. The main difference between LET and LETREC is ______________________ ____________________________________________________________________. Give an example of using LETREC to avoid passing unchanging parameters.