LISP:Program structure in lisp


Published on

LISP:Program structure in lisp

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

LISP:Program structure in lisp

  1. 1. Program structure in Lisp<br />
  2. 2. overview<br />Forms<br />Variables<br />Macros<br />Function calls<br />Named functions<br />Lambda expressions<br />Top-level forms<br />Declaring global variables and named constants<br />Control of time and evaluation<br />
  3. 3. forms<br />Forms are the standard units of iteration in CL implementation.<br />A form is simply a data object meant to be evaluated as a program to produce one or more values.<br />Meaning full forms may be further divided into:<br /><ul><li>Self-evaluating forms(numbers)
  4. 4. Symbols(variables)
  5. 5. Lists</li></ul>Lists can further be divided as: special forms, macro calls and function calls.<br />All characters, numbers, string, and bit-vectors are all self-evaluating forms.<br />When such an object is evaluated, that object is returned as the value of the form.<br />
  6. 6. Variables<br />Symbols are used as the names for the variables in CL programs.<br />When a symbol is evaluated as the form, the value of the variable it names is produced.<br />Ex: (setq w 8)produces 8<br />Types of variables:<br /><ul><li>Lexical (static)
  7. 7. Special (dynamic)</li></li></ul><li>Special forms<br />Common lisp special forms:<br />While the lisp is being evaluated as a form, if the first element is one of the element in the above table, then it is called a special form.<br />Ex: (if p (+ x 4) 5) means if p then x+4 else 5<br />block if progv<br />catch labels quote<br />[compiler-let] let return-from<br />declare let* setq<br />eval-when macrolet tagbody<br />flet multiple-value-call the<br />function multiple-value-prog1 throw<br />go progn unwind-protect<br />
  8. 8. It is recommended that the program-analyzing program process a form that is a list whose car is a symbol as follows:<br /><ul><li>If the program has particular knowledge about the symbol, process the form using special –purpose code. All the symbols in the previous table fall under this category.
  9. 9. If the macro-function is true of the symbol, apply either macro-expand or macroexpand-1, as appropriate to the entire form and then start over.
  10. 10. Otherwise, assume it as a function call.</li></li></ul><li>Macros<br />A macro is essentially a function from forms to forms.<br />Given a call to a macro, compute a new form to be evaluated in the place of the macro call.<br />There are a number of standard forms in CL, to define a new form use defmacro.<br />
  11. 11. Function calls<br />While the list is being evaluated as a form, if the first element is not a symbol that names a special form or a macro, the it is considered as a function call.<br />The first element of the list is taken as the name of the function.<br />All the remaining elements of the list are evaluated and one value is obtained from each form, and these values become the arguments to the function.<br />Whatever values the function returns becomes the values of the function-call forms. <br />
  12. 12. Function calls continued..<br />Ex: (+ 3 (* 1 2)) here, + is not a special form or macro. So 3 and (* 1 2) are considered as arguments of the function call, again 1 2 are taken as arguments of the function call (* 1 2) and the value 2 is returned. The values 2 and 3 are given as arguments for the addition function to produce the result 5. <br />Thus, ( + 3 (* 1 2))5<br />
  13. 13. Functions<br />Two ways to indicate the function as the function-call form are:<br /><ul><li> using symbols to name the functions
  14. 14. using a lambda-expression whose first element in the list is lambda.</li></ul>Lambda expressions or the symbols when used in programs as the name of the functions, can appear only as the first element of the function-call form, or the second element of the function special form.<br />A global name can be given to a function using the defun construct.<br />A local name is given to the function using the flet or labels construct.<br />
  15. 15. Lambda-expressions<br />A lambda-expression has the following syntax:<br />(lambda lambda-list .body)<br />The first element must be lambda, the second element must be the list, and specifies names for the parameters of the list<br />The body may consists of zero or more number of forms.<br />Each element in the lambda-list is either a parameter specifier or the lambda-list keyword.<br />(lambda-list keywords begin with &)<br />Var or svar must be a symbol( the name of the variable),<br />Each keyword must be a keyword symbol, such as :start<br />
  16. 16. A lambda-list has five parts<br />Specifiers for the required parameters: these are parameter specifiers up to the first lambda-list keyword.<br />Specifiers for the optional parameters: these are the specifiers following the lambda-list keyword &optional up to the next lambda-list keyword or the end of the list.<br />A specifier for a rest parameter<br />Specifiers for the keyword parameters<br />Specifiers for the aux parameters: If the lambda-list keyword &key is present, all specifiers after it are auxiliary variable specifiers.<br />
  17. 17. Top-level forms<br />The standard way to interact with CL implementation is via a read-eval-print loop.<br />The system reads a form from the same input source, evaluates it and then prints the value to the output sink.<br />Top-level special forms are used:<br /><ul><li>To define globally named functions
  18. 18. Define macros
  19. 19. Make declarations
  20. 20. Define global values for special variables</li></ul>If progn appears at top-level, then all forms within that progn are considered by the compiler to be top-level forms.<br />
  21. 21. Defining named functions<br />defun special form is used for defining named functions.<br /> evaluating the defun form causes the symbol name to be a global name for the function specified by the lambda-expression.<br />(defun (setf cadr)…) is used to define a setf expansion function to cadr.<br />Forms constitute the body of the defined function, they are executed as an implicit progn.<br />defun name lambda-list [[ {declaration}* | doc-string]] {form}*<br />
  22. 22. Declaring global variables and named constants<br />The following special forms are used to specify globally defined variables:<br /><ul><li> defvar
  23. 23. defparameter
  24. 24. defconstant (used for defining named constants)</li></ul>Syntax:<br />defvar name [initial-value [documentation]]<br />defparameter name initial-value [documentation]<br />defconstant name initial-value [documentation]<br />
  25. 25. (defvar variable) proclaims the variable to be special.<br />defvar also gives a good option to provide the comment for the variable<br />Ex: (defvar *visible-windows* 0<br /> “number of visual windows at least partially visible on the screen”)<br />defparameter is similar to defvar, but it requires an initial value-form, always evaluates the form, and assigns the result to the variable.<br />defconstant is like defparameter but does assert that the value of the variable name is fixed and does license the compiler to build assumptions about the value into program being compiled.<br />
  26. 26. Control of time and execution<br />The eval-when special code allows pieces of code to be executed only at compile-time, only at load time, or when interrupted but not compiled.<br />eval-when ({situation}*) {from}*<br />The body of the eval-when form is processed as an implicit progn only if the situation is a symbol, either compile, eval or load.<br />Eval specifies that the interpreter should process the body.<br />Compile specifies that the compiler should evaluate the body at compile time.<br />Load specifies that the compiler should arrange to evaluate the forms in the body when the compiled file containing the eval-when form is loaded.<br />
  27. 27. Procedure for processing a form:<br />If the form is an eval-form<br /><ul><li>if the situation load is specified:
  28. 28. if the situation compile is specified, or if the current processing mode is compile-time-too and the situation eval is specified, the process each of the forms in the body in compile-time-too mode.
  29. 29. else, process each of the forms in the body in not-compile-time mode
  30. 30. if the situation load is not specified:
  31. 31. if the situation compile is specified, or if the current processing mode is compile-time-too and the situation eval is specified, then evaluate each of the forms in the body in the compiler’s executing environment.
  32. 32. else, ignore the eval-when form entirely</li></li></ul><li>Ex: the use of eval-when is that if the compiler is to be able to properly read a file that uses user defined reader macro characters , it is necessary to write<br />(eval-when (compile load eval)<br /> (set-macro-character #$ #’(lambda (stream char)<br /> (declare (ignore char))<br /> (list ‘dollar (read stream)))))<br /> This causes the call to set-macro-character to be executed in the compiler’s execution environment, thereby modifying its readers syntax table.<br />
  33. 33. 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 />