Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bindings, Scope, and Extent

Similar presentations


Presentation on theme: "Bindings, Scope, and Extent"— Presentation transcript:

1 Bindings, Scope, and Extent
Global bindings Local bindings and lexical scope Function bindings LET vs LET* 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 Lisp object such as the global value of the symbol or the function binding 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. (defun 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. (defun fumble (y) (setq 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
Symbols are normally grouped into packages. A package represents a mapping between a set of symbols and their various bindings within the package. In simple programs, we can usually ignore the fact that there are different packages. Allegro Common Lisp V5.0 > (setq x 5) ; What’s going on here? 5 X is a symbol which is registered (“interned”) in the current package. Its global value (“symbol value”) has been set to 5. Its symbol-value 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. > (setq x 99) ; Establishes a global binding 99 > (defun foo (x) (+ x 2)) ; Makes a local one FOO > (foo 2000) 2002 > (let ((x 15)) (print x)) ; Another local one 15 NIL > 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 NIL > (defun fact (x) (if (= x 1) 1 (* x (fact (1- x)))) ) FACT > (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 (“symbol value”) can be reached, even within the scopes of local bindings. > (setq x 77); global 77 > (defun foo (x) (+ x (symbol-value 'x)) ) FOO > (foo 1900) 1977 CSE S. Tanimoto Bindings, Scope, and Extent

9 CSE 341 -- S. Tanimoto Bindings, Scope, and Extent
“Unbound” Variables A symbol that has not been given a value by assignment or in a function call is said to be “unbound.” > (+ newsymbol 5) ERROR Unbound variable NEWSYMBOL. The use of “unbound” here is actually a misnomer, because NEWSYMBOL has a binding. The binding just doesn’t have any value. But old uses of terminology persists! > (setq newsymbol 5) 5 > (makunbound 'newsymbol); undo the assignment NEWSYMBOL CSE S. Tanimoto Bindings, Scope, and Extent

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

11 Comparisons for Lexical Variables and Functions
Normal, lexical variables have lexical scope and indefinite extent [via closures]. Symbol-to-Function bindings established with DEFUN have indefinite scope and indefinite extent. Dynamic variables 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

12 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) (setq w (+ x y z) ) (print w) ) 45 > (let* ((x 5)(y (* x 2)) (z (* y 3)) w) (setq w (+ x y z)) 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, functions, etc.) is effective. Examples: Symbols and their SYMBOL-VALUE bindings in the Common-Lisp-User package Symbols and their operator (functions and macros) bindings in a Lisp graphics package. Lisp’s main means of organizing name spaces are: Packages and Binding types (value vs operator)  An alternative definition of a name space is a coherent set of possible names that can be used to unambiguously reference some coherent set of possible values, functions, etc. CSE S. Tanimoto Bindings, Scope, and Extent


Download ppt "Bindings, Scope, and Extent"

Similar presentations


Ads by Google