Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

LISP: Scope and extent in lisp


Published on

LISP: Scope and extent in lisp

Published in: Technology, Business
  • Login to see the comments

  • Be the first to like this

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<br />