• Save
LISP:Program structure in lisp
Upcoming SlideShare
Loading in...5
×
 

LISP:Program structure in lisp

on

  • 3,159 views

LISP:Program structure in lisp

LISP:Program structure in lisp

Statistics

Views

Total Views
3,159
Views on SlideShare
3,155
Embed Views
4

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 4

http://www.slideshare.net 4

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

LISP:Program structure in lisp LISP:Program structure in lisp Presentation Transcript

  • Program structure in Lisp
  • overview
    Forms
    Variables
    Macros
    Function calls
    Named functions
    Lambda expressions
    Top-level forms
    Declaring global variables and named constants
    Control of time and evaluation
  • forms
    Forms are the standard units of iteration in CL implementation.
    A form is simply a data object meant to be evaluated as a program to produce one or more values.
    Meaning full forms may be further divided into:
    • Self-evaluating forms(numbers)
    • Symbols(variables)
    • Lists
    Lists can further be divided as: special forms, macro calls and function calls.
    All characters, numbers, string, and bit-vectors are all self-evaluating forms.
    When such an object is evaluated, that object is returned as the value of the form.
  • Variables
    Symbols are used as the names for the variables in CL programs.
    When a symbol is evaluated as the form, the value of the variable it names is produced.
    Ex: (setq w 8)produces 8
    Types of variables:
    • Lexical (static)
    • Special (dynamic)
  • Special forms
    Common lisp special forms:
    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.
    Ex: (if p (+ x 4) 5) means if p then x+4 else 5
    block if progv
    catch labels quote
    [compiler-let] let return-from
    declare let* setq
    eval-when macrolet tagbody
    flet multiple-value-call the
    function multiple-value-prog1 throw
    go progn unwind-protect
  • It is recommended that the program-analyzing program process a form that is a list whose car is a symbol as follows:
    • 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.
    • 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.
    • Otherwise, assume it as a function call.
  • Macros
    A macro is essentially a function from forms to forms.
    Given a call to a macro, compute a new form to be evaluated in the place of the macro call.
    There are a number of standard forms in CL, to define a new form use defmacro.
  • Function calls
    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.
    The first element of the list is taken as the name of the function.
    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.
    Whatever values the function returns becomes the values of the function-call forms.
  • Function calls continued..
    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.
    Thus, ( + 3 (* 1 2))5
  • Functions
    Two ways to indicate the function as the function-call form are:
    • using symbols to name the functions
    • using a lambda-expression whose first element in the list is lambda.
    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.
    A global name can be given to a function using the defun construct.
    A local name is given to the function using the flet or labels construct.
  • Lambda-expressions
    A lambda-expression has the following syntax:
    (lambda lambda-list .body)
    The first element must be lambda, the second element must be the list, and specifies names for the parameters of the list
    The body may consists of zero or more number of forms.
    Each element in the lambda-list is either a parameter specifier or the lambda-list keyword.
    (lambda-list keywords begin with &)
    Var or svar must be a symbol( the name of the variable),
    Each keyword must be a keyword symbol, such as :start
  • A lambda-list has five parts
    Specifiers for the required parameters: these are parameter specifiers up to the first lambda-list keyword.
    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.
    A specifier for a rest parameter
    Specifiers for the keyword parameters
    Specifiers for the aux parameters: If the lambda-list keyword &key is present, all specifiers after it are auxiliary variable specifiers.
  • Top-level forms
    The standard way to interact with CL implementation is via a read-eval-print loop.
    The system reads a form from the same input source, evaluates it and then prints the value to the output sink.
    Top-level special forms are used:
    • To define globally named functions
    • Define macros
    • Make declarations
    • Define global values for special variables
    If progn appears at top-level, then all forms within that progn are considered by the compiler to be top-level forms.
  • Defining named functions
    defun special form is used for defining named functions.
    evaluating the defun form causes the symbol name to be a global name for the function specified by the lambda-expression.
    (defun (setf cadr)…) is used to define a setf expansion function to cadr.
    Forms constitute the body of the defined function, they are executed as an implicit progn.
    defun name lambda-list [[ {declaration}* | doc-string]] {form}*
  • Declaring global variables and named constants
    The following special forms are used to specify globally defined variables:
    • defvar
    • defparameter
    • defconstant (used for defining named constants)
    Syntax:
    defvar name [initial-value [documentation]]
    defparameter name initial-value [documentation]
    defconstant name initial-value [documentation]
  • (defvar variable) proclaims the variable to be special.
    defvar also gives a good option to provide the comment for the variable
    Ex: (defvar *visible-windows* 0
    “number of visual windows at least partially visible on the screen”)
    defparameter is similar to defvar, but it requires an initial value-form, always evaluates the form, and assigns the result to the variable.
    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.
  • Control of time and execution
    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.
    eval-when ({situation}*) {from}*
    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.
    Eval specifies that the interpreter should process the body.
    Compile specifies that the compiler should evaluate the body at compile time.
    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.
  • Procedure for processing a form:
    If the form is an eval-form
    • if the situation load is specified:
    • 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.
    • else, process each of the forms in the body in not-compile-time mode
    • if the situation load is not specified:
    • 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.
    • else, ignore the eval-when form entirely
  • 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
    (eval-when (compile load eval)
    (set-macro-character #$ #’(lambda (stream char)
    (declare (ignore char))
    (list ‘dollar (read stream)))))
     This causes the call to set-macro-character to be executed in the compiler’s execution environment, thereby modifying its readers syntax table.
  • Visit more self help tutorials
    Pick a tutorial of your choice and browse through it at your own pace.
    The tutorials section is free, self-guiding and will not involve any additional support.
    Visit us at www.dataminingtools.net