1 / 12

Lexical Variables and Scope

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

merlin
Download Presentation

Lexical Variables and Scope

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 480/680 – Comparative Languages Lexical Variables and Scope

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

More Related