Your SlideShare is downloading. ×
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros in lisp
LISP: Macros 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: Macros in lisp


Published on

LISP: Macros in lisp

LISP: Macros in lisp

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Macros in Lisp
  • 2. CL macro facility allows the user to define arbitrary functions that convert certain lisp forms into different forms before evaluating or compiling them.
    Macros makes it possible to write codes that are clear and elegant.
    In CL, macros are not functions
    In particular, macros cannot be used as the functional arguments to functions such as apply, funcall, or map.
  • 3. overview
    Macro definition
    Macro expansion
    Compiler macros
  • 4. Macro definition
    defmacro construct is used to define a macro.
    Macro-function determines whether the given function is the name of the macro.
    macro-function symbol
    The argument must be a symbol, if the function has a global function definition( macro definition) then the expansion function is returned else nil is returned.
    Macro-function cannot work on local variables it can only determine the global definitions.
    Setf is used as a macro function to install a macro as a symbols global function definition
    To add an optional environment argument to macro-function:
    macro-function symbol &optional env
    (setf (macro-function symbol) fn)
  • 5. defmacro has the same syntax as the defun
    Here, name is the symbol whose macro definition we are creating.
    Lambda-list is similar in form to a lambda-list,
    Forms constitute the body of the expander function.
    The defmacro function returns the name.
    defmacro name lambda-list [[ {declaration}* | doc-string ]] {form}* [Macro]
  • 6. Defmacro lambda-list keywords
    Some of the lambda-list key words are: &optional, &rest, &key, &allow-other-keys, &aux
    &body: this is identical in function to &rest, it informs certain output-formatting and editing functions that the remainder of the form is treated as a body and should be intended accordingly.
    &whole: this is followed by a single variable that is bound to the entire macro-call form.
    &environment: this is followed by a single variable that is bound to an environment representing the lexical environment in which the macro call is to be interpreted.
  • 7. Macro-expansion
    If form is a macro call, then macroexpand-1 will expand the macro call once and return two values.( the expansion and t) if form is not macro call then two values ( form and nil) are returned.
    A form is said to be a macro call only if it is a cons whose car is a symbol that names a macro.
    The macro expansion function is used for expanding a macro call.
  • 8. Macro-expansion steps followed
    Once the macroexpand-1 has determined that a symbol names a macro, it obtains the expansion function for that macro.
    The value of the variable *macro-expand-hook* is then called as a function of three arguments: the expansion function, the form, and the environment env.
    The value returned from this call is taken to be the expansion of the macro call.
    The evaluator makes use of the macro calls as if through the use of macroexpand-1; eval also makes use of *macroexpand-hook*
    Macroexpand is similar to macroexpand-1, but repeatedly expands forms until it is no longer a macro call.
  • 9. Destructing
    To make the destructing feature of the defmacro available use:
    This macro binds the variables specified in lambda-list to the corresponding values in the tree structure resulting from evaluating the expression, then executes the forms as the an implicit progn.
    If the result of the evaluating expression does not match the destructuring pattern, an error should be signaled.
    Destructuring-bind lambda-list expression {declaration}* {form}[Macro]
  • 10. Compiler macros
    Compiler macros are used to transform the selective source code when the compiler is processing the code.
    This is similar to defmacro except the definition is not stored in the symbol function cell of name and is not seen by macroexpand-1
    The lambda-list may include &environment, &whole and may include destructuring.
    Return-form is used to exit from the function.
    define-compiler-macro name lambda-list
    {declaration | doc-string}* {form}*
  • 11. Name must be a symbol, if it has been defined as a compiler macro, then the compiler-macro-function returns the macro expansion function, else returns nil.
    Expander function is obtained as if by a call to compiler-macro-function on the car of the form rather than by a call to macro-function.
    Two values are returned, the expansion and the value if expansion is true else nil.
    compiler macro function name &optional env
    compiler-macroexpand from &optional env
    compiler-macroexpand-1 from &optional env
  • 12. Three cases when no expansion occurs:
    There is no compiler macro definition for the car of the form.
    There is such a definition but there is also a not inline declaration, either globally or in lexical environment env.
    A global compiler macro definition is shadowed by a local function or macro definition.
  • 13. Environments
    There is a minimal set of accessors and a constructor for environments.
    Define-declaration is used to define all the standard declaration specifiers, with the exception of special.
    The function returns the interpretation of the symbol variable when it appears as a variable within the lexical environment env.
    Three values are returned:
    The first value indicates the type of definition in the env which may be :special, :lexical, :symbol-macro, :constant
    Variable-information variable &optional env
  • 14. 2. The second value is true if there is a local binding else it returns nil.
    3. The third value is an a-list containing information about the declarations that apply to the apparent binding of the variable.
    Returns the information about the interpretation of the function when it appears in the functional position of the lexical environment env.
    This function also returns three values:
    Type of definition or binding of the function name which is apparent in env (nil, :function, :macro, :special-form)
    The second value is true if the function is local else nil.
    The third value is an a-list containing information about declarations that apply to the apparent binding of the function.
    function-information function &optional env
  • 15. argument-environment env &key :variable :symbol-macro :fuction :macro :declare
    returns a new environment containing the information present in enc augmented with the information provided by the key word arguments.
    The arguments used are:
    • :variable list of symbols that will be visible as bound variable in the new environment,.
    • 16. :symbol-macro  list of symbol macro definitions
    • 17. :function  list of function-names that will be visible as local function bindings in the new environment.
    • 18. :macro list of local macro definitions
    • 19. :declare list of declaration specifiers
  • define-declaration decl-name lambda-list {form}*
    The macro defines a macro for the named declaration.
    The function defined by this macro will be called with two augments, a declaration specifier whose car is decl-name and the env argument to augment-environment.
  • 20. The function must return two values:
    • the first value must be one of the following keywords :variable, :function, :declare
    • 21. If the first value is :variable or :function, then the second value must be a list, the elements of which are of the form (binding-name key value)
    • 22. If the first value is :declare, the second value must be a cons of the form ( key. Value)
  • parse-macro name lambda-list body &optional env
    The function is used to process a macro definition, it returns a lambda-expression that accepts two argument, a form and an environment.
    The function returns an object of type function that is equivalent to what would be obtained by evaluating ‘(function, lambda-expression) in a syntactic environment env.
  • 23. 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