120 likes | 218 Views
CS 480/680 – Comparative Languages. Lexical Variables and Scope. Variables and Scope. (define x 7) – global variable (define add2 (lambda (x) (+ x 2))) – the parameter x is a local variable in the function definition
E N D
CS 480/680 – Comparative Languages Lexical Variables and Scope
Variables and Scope • (define x 7) – global variable • (define add2 (lambda (x) (+ x 2))) – the parameter x is a local variable in the function definition • (add2 x) » 9 – The call to add2 uses the global x, but within the definition the local x is used. • No call to add2 will change the global x Variables and Scope
Local Variables • Let can be used to introduce local variables: • (let ((var val) (var val)…) (body)) (let ((x 1) (y 2) (z 3)) (list x y z)) » (1 2 3) Variables and Scope
Let and Lambda • Let is just syntactic sugar for lambda • Anyone see how? (let ((x 1) (y 2) (z 3)) (list x y z)) ((lambda (x y z) (list x y z)) 1 2 3 ) Variables and Scope
The “Let” form • The initializers in the let form are not considered part of the let body • Their scope is “one level up” (define (x 20)) (let ((x 1) (y x)) (+ x y)) » 21 Refers to the global x (20), not the local one (1). Variables and Scope
Let* • Sometimes you want to introduce variables in sequence, so that the scope of each variable includes previously defined local variables. This is what let* does… (define (x 20)) (let* ((x 1) (y x)) (+ x y)) » 2 Variables and Scope
Binding procedures • Just like global variables, local variables (symbols) can be bound to procedures too… (let ((cons (lambda (x y) (+ x y)))) (cons 1 2)) » 3 Overrides the global binding for cons inside the body of the let form. Variables and Scope
Scope • Procedures can access any variable in their surrounding scope that they don’t shadow: (define counter 0) (define bump-counter (lambda () (set! counter (+ counter 1)) counter)) (bump-counter) » 1 (bump-counter) » 2 Variables and Scope
Static Scope • Procedure scope is determined when the procedure is defined not when it is called (let ((counter 99)) (display (bump-counter)) (newline) (display (bump-counter)) (newline) (display (bump-counter)) (newline)) 3 4 5 Variables and Scope
Closure • Lambda returns a closure: a procedure as well as the current set of parameter bindings. (define counter 0) (define bump-counter (lambda () (set! counter (+ counter 1)) counter)) (bump-counter) » 1 (bump-counter) » 2 Variables and Scope
Fluid-let • Fluid-let temporarily modifies global varaibles • They return to their previous values after the form ends (fluid-let ((counter 99)) (display (bump-counter)) (newline) (display (bump-counter)) (newline) (display (bump-counter)) (newline)) 100 101 102 Variables and Scope
Exercises • Using your function that returns the average of three numbers (from the previous lecture exercises)… • Define three global variables and call the function using the global values • Define three local variables (using let) with the same names as the global variables above, and attempt to call your function passing the value of the three local variables • Since the previous operation will fail, use fluid-let to achieve the same thing Variables and Scope