Your SlideShare is downloading. ×
0
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
LISP:Program structure in lisp
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

LISP:Program structure in lisp

2,589

Published on

LISP:Program structure in lisp

LISP:Program structure in lisp

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,589
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Program structure in Lisp<br />
  • 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. 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. Symbols(variables)
  • 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. 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. 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. 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. 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. 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. 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. 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. Functions<br />Two ways to indicate the function as the function-call form are:<br /><ul><li> using symbols to name the functions
  • 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. 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 &amp;)<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. 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 &amp;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 &amp;key is present, all specifiers after it are auxiliary variable specifiers.<br />
  • 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. Define macros
  • 19. Make declarations
  • 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. 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. Declaring global variables and named constants<br />The following special forms are used to specify globally defined variables:<br /><ul><li> defvar
  • 23. defparameter
  • 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. (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. 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. Procedure for processing a form:<br />If the form is an eval-form<br /><ul><li>if the situation load is specified:
  • 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. else, process each of the forms in the body in not-compile-time mode
  • 30. if the situation load is not specified:
  • 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. 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. 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 />

×