Go to the first, previous, next, last section, table of contents.


Macro Expansion

When a macro call is detected (see section List Forms) the function which is the cdr of the macro's definition (see section Defining Macros) is applied to the macro call's arguments. Unlike in a function call, the arguments are not evaluated, the actual forms are the arguments to the macro's expansion function. This is so these forms can be rearranged by the macro's expansion function to create the new form which will be evaluated.

There is a function which performs macro expansion, its main use is to let the Lisp compiler expand macro calls at compile time.

Function: macroexpand form &optional environment
If form is a macro call macroexpand will expand that call by calling the macro's expansion function (the cdr of the macro definition). If this expansion is another macro call the process is repeated until an expansion is obtained which is not a macro call, this form is then returned.

The optional environment argument is an alist of macro definitions to use as well as the existing macros; this is mainly used for compiling purposes.

(defmacro when (condition &rest body)
  "Evaluates condition, if it's non-nil evaluates the body
forms."
  (list 'if condition (cons 'progn body)))
    => when

(macroexpand '(when x (setq foo bar)))
    => (if x (progn (setq foo bar)))


Go to the first, previous, next, last section, table of contents.