Determiners of scope<br />The circumstance which determines scope in CommonLisp includes:<br /><ul><li>The location of reference within an expression.
The kind of expression in which the reference takes place.
The location of the reference within the program text.
The specific instance of the program within which the reference is resolved.</li></li></ul><li>Common lisp programmers must know :<br />What kind of reference is being used<br />What kind of scope it uses if it is a variable reference<br />In what environment is the reference solved<br />Where was the binding introduced etc..<br />
<ul><li> In common lisp, the notations Scope and extent are used when some object or constraint has to be referred to or from a distant part of the program.
Scope refers to textual or spatial region of the program within </li></ul>which references may occur.<br /><ul><li>Extent refers to the interval of time through which references may occur.</li></ul>Ex: (defun copy-cell (x) (cons (car x) (cdr x)))<br />Here, the scope of the parameter x is the body of the defun form.<br />The extent of the form is the interval form the time the function is invoked to the time it is exited.<br />
Different kinds of scope and extent used in Common Lisp<br /><ul><li>Lexical scope: references to the established entity can occur only within certain program portions that are lexically contained within the establishing construct.</li></ul>Ex: names of the parenthesis to a function are lexically scoped.<br /><ul><li>Indefinite scope: references may occur anywhere in the program.
Dynamic extent: references may occur at any time between the establishment of the entity and the explicit disestablishment of the entity.
Indefinite extent: this entity continues to exist as long as the possibility of the reference remains.</li></ul>Most common Lisp data objects have indefinite extent.<br />
<ul><li>In case of lexical scope, if two constructs that establish entities with the same name are textually nested, then references within the inner construct refer to the entity established by the inner one, the inner one shadows the outer one.</li></ul>Ex: (defun test(x z)<br /> (let ((z (* x 2))) (print z))<br /> z)<br />Here, binding of the variable z by the let construct shadows the parameter binding for the function test. The reference to the variable z at the print form refers to the let binding. The reference to z at the end of the function refers to the parameter named z.<br />
Ex: ( defun compose( f g)<br /># ‘ (lambda (x)<br /> (funcall f (funcall g x))))<br />The parameter bindings for f and g do not disappear because the returned function, when called, could still refer to those bindings.<br />( funcall (compose #’ sqrt # ‘ abs) -9.0)<br />Produces the value 3.0 <br />
LET variables<br /><ul><li>Objects bound by function parameters and LET forms have lexical scope.
Their bindings are visible beginning at a certain textual location in the defining form and continuing through the textual end of the defining form.
Any reference to a textually identical name from outside of the defining name must refer to a different binding.
A nested defining form may declare a binding that shadows an enclosing binding to a textually identical name. </li></li></ul><li>Special variables<br /><ul><li>Special variables (dynamic variables) have dynamic scope.
A binding is created for a special variable as the result of the executing some form in your program.
The scope of the dynamic form extends into any form ( called directly or indirectly) by the form which established the dynamic binding.
The extent of the dynamic form exists indefinitely, until the dynamic form finishes executing.</li></li></ul><li>Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />