Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.

Similar presentations


Presentation on theme: "CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent."— Presentation transcript:

1 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent Global bindings Local bindings LET vs LET* Lexical vs Dynamic variables

2 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 2 Definition of Binding Binding: A binding is 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. “Binding” may refer to … a symbol and its value in a particular context; (math definition) a symbol and the memory location used to store one of its values; (Lisp definition) the memory location associated with a symbol, to hold (one of) its value(s). (informal usage in talking about programming languages).

3 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 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.

4 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 4 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.

5 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 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. hilbert% clisp > (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.

6 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 6 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.

7 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 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

8 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 8 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

9 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 9 “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 persist! > (setq newsymbol 5) 5 > (makunbound 'newsymbol); undo the assignment NEWSYMBOL

10 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 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) )

11 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 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) ].

12 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 12 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)) (print w) ) 45


Download ppt "CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent."

Similar presentations


Ads by Google