Download presentation
Presentation is loading. Please wait.
1
Bindings, Scope, and Extent
Global bindings Local bindings and lexical scope Function bindings LET vs LET* and LETREC Name spaces CSE S. Tanimoto Bindings, Scope, and Extent
2
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Binding: an association between a symbol and a memory location that holds another Scheme object such as the top-level value of the symbol. CSE S. Tanimoto Bindings, Scope, and Extent
3
Scoping and Extent of Bindings
Scope: The region of a program in which a binding is available. (define (foo x) ; X gets bound in the call. (+ x 2) ; The binding is only ) ; available here. CSE S. Tanimoto Bindings, Scope, and Extent
4
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
Extent of Bindings Extent: The span of time during which a binding is in effect. (define z 0) (define (fumble y) (set! z 2) ; Z has indefinite extent (+ z y) ; Y’s extent is the period ) ; during which the body ; of FUMBLE is being executed. CSE S. Tanimoto Bindings, Scope, and Extent
5
Global Values of Symbols
The collection of active global variables and their values represents a mapping between a set of symbols and their various bindings. In simple programs, we can usually ignore the fact that there are different packages. Welcome to DrScheme, version 201 > (define x 5) ; What’s going on here? X is a symbol which is registered (“interned”) in the Scheme session. Its global value has been set to 5. Its global binding has indefinite extent. It has global scope. CSE S. Tanimoto Bindings, Scope, and Extent
6
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
Local Bindings Certain constructs cause creation of new local bindings. > (define x 99) ; Establishes a global binding ; when entered at the top level. > (define (foo x) (+ x 2)) ; Makes a local one > (foo 2000) 2002 > (let ((x 15)) (print x)) ; Another local one 15 > x 99 ; The global binding was not disturbed. CSE S. Tanimoto Bindings, Scope, and Extent
7
Stacking of Local Bindings
New local bindings are independent of existing local bindings. > (let ((x 1)) (let ((x 2)) (print x) ) ;Here X has 2 local bindings (print x) ) ;Here X has only 1 local b. 2 1 > (define (fact x) (if (= x 1) 1 (* x (fact (- x 1)))) ) > (fact 4) ;X will get 4 simultaneous bindings 24 CSE S. Tanimoto Bindings, Scope, and Extent
8
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
Hiding of Bindings The most recent existing local binding hides all other existing local bindings. The global value can be reached, even within the scopes of local bindings. > (define x 77); global 77 > (define (getx) x) > (define (foo x) (+ x (getx)) ) > (foo 1900) 1977 CSE S. Tanimoto Bindings, Scope, and Extent
9
Dynamic Scope with Special Variables
Older dialects of Lisp did not limit the scopes of variables to function bodies or other restricted regions of the program. Variable values could be accessed anywhere in the program, provided the bindings were still in effect and were not shadowed by other bindings of the same symbols. Common Lisp supports this dynamic scoping if you ask for it. Scheme adheres strictly to lexical scoping. (Common Lisp Example -- Not Scheme!) (defun double (x) (declare (special x)) (announce) (+ x x) ) (defun announce () (format t "The value of X is ~A." x) ) CSE S. Tanimoto Bindings, Scope, and Extent
10
Comparisons for Lexical Variables and Functions
Normal, lexical variables have lexical scope and indefinite extent [via closures]. Top-level bindings established with DEFINE have indefinite scope and indefinite extent. Dynamic variables (not available in Scheme) have indefinite scope and limited extent. [Limited by duration of evaluation of the construct that establishes the binding, e.g., by (double 2) ]. CSE S. Tanimoto Bindings, Scope, and Extent
11
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
LET* vs LET LET (“Parallel”) begins the scope of each local binding at the first form after all the local bindings have been established. LET* (“Sequential”) begins the scope of each local binding immediately after its initial assignment. > (let ((x 5) (y 10) (z 30) w) (set! w (+ x y z) ) (print w) ) 45 > (let* ((x 5) (y (* x 2)) (z (* y 3)) w) (set! w (+ x y z)) CSE S. Tanimoto Bindings, Scope, and Extent
12
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
LETREC LETREC (“Parallel, recursive”) begins all the scopes of the local bindings at the beginning of the list in which they are established established. This permits local recursive procedures to be created. > (letrec ((addup (lambda (x) (if (= x 0) 0 (+ x (addup (- x 1))) ) ) )) (print (addup 4)) ) 10 > (addup 4) reference to undefined identifier: addup CSE S. Tanimoto Bindings, Scope, and Extent
13
CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
Name Spaces A name space is a (dynamic) set of names (strings) in which a particular corresponding (dynamic) set of bindings (e.g., values, procedures, etc.) is effective. Examples: Symbols and their global bindings. Symbols and their local bindings within a procedure. Lexical scoping allows separation of name spaces. CSE S. Tanimoto Bindings, Scope, and Extent
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.