LISP: Scope and extent in lisp

703 views

Published on

LISP: Scope and extent in lisp

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
703
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

LISP: Scope and extent in lisp

  1. 1. Scope and Extent in Lisp<br />
  2. 2. Determiners of scope<br />The circumstance which determines scope in CommonLisp includes:<br /><ul><li>The location of reference within an expression.
  3. 3. The kind of expression in which the reference takes place.
  4. 4. The location of the reference within the program text.
  5. 5. 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 />
  6. 6. <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.
  7. 7. 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 />
  8. 8. 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.
  9. 9. Dynamic extent: references may occur at any time between the establishment of the entity and the explicit disestablishment of the entity.
  10. 10. 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 />
  11. 11. <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 />
  12. 12. 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 />
  13. 13. LET variables<br /><ul><li>Objects bound by function parameters and LET forms have lexical scope.
  14. 14. Their bindings are visible beginning at a certain textual location in the defining form and continuing through the textual end of the defining form.
  15. 15. Any reference to a textually identical name from outside of the defining name must refer to a different binding.
  16. 16. 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.
  17. 17. A binding is created for a special variable as the result of the executing some form in your program.
  18. 18. The scope of the dynamic form extends into any form ( called directly or indirectly) by the form which established the dynamic binding.
  19. 19. 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 />

×