DEFMACRO(3cl) |
Common Lisp Reference |
DEFMACRO(3cl) |
NAME
defmacro – define a macro by associating a name with a macro function (macro)
SYNOPSIS
defmacro
|
ARGUMENTS and VALUES
—a symbol.
—a macro lambda list.
declare expression; not evaluated. —a
—a string, not evaluated.
—a form.
DESCRIPTION
Defines defmacro form appears. as a macro by associating a macro function with that name in the global environment. The macro function is defined in the same lexical environment in which the
The parameter variables in are bound to destructured portions of the macro call.
The expansion function accepts two arguments, a form and an environment. The expansion function returns a form. The body of the expansion function is specified by s. s are executed in order. The value of the last executed is returned as the expansion of the macro. The body s of the expansion function (but not the ) are implicitly enclosed in a block whose name is .
The conforms to the requirements of macro lambda lists.
is attached as a documentation string to (as kind function) and to the macro function.
defmacro can be used to redefine a macro or to replace a function definition with a macro definition.
Recursive expansion of the returned must terminate, including the expansion of other macros which are subforms of other forms returned.
The consequences are undefined if the result of fully macroexpanding a contains any circular list structure except in literal objects.
If a defmacro form appears as a top level form, the compiler must store the macro definition at compile time, so that occurrences of the macro later on in the file can be expanded correctly. Users must ensure that the body of the macro can be evaluated at compile time if it is referenced within the file being compiled .
AFFECTED BY
(none)
EXCEPTIONAL SITUATIONS
(none)
NOTES
(none)
EXAMPLES
(defmacro () "Mac1 multiplies and adds"
`(+ , (* , 3)))
( 4 5) 19
(documentation ’ ’function) "Mac1 multiplies and adds"
(defmacro (&optional ( 2 ) ( 3 ) &rest )
`’(, , , , ,))
( 6) (6 T 3 NIL NIL)
( 6 3 8) (6 T 3 T (8))
(defmacro (&whole &optional ( 3)
&rest &key ())
`’(, , , , , ,))
( 1 6 : 8 : 9 : 10)
→ (( 1 6 : 8 : 9 : 10) 1 6 9 8 (: 8 : 9 : 10))
The stipulation that an embedded destructuring lambda list is permitted only where ordinary lambda list syntax would permit a parameter name but not a list is made to prevent ambiguity. For example, the following is not valid:
(
defmacro
(
&optional
(
&rest
)
&rest
)
...
)
because ordinary lambda list syntax does permit a list following &optional; the list ( &rest ) would be interpreted as describing an optional parameter named whose default value is that of the form , with a parameter named &rest (not valid), and an extraneous symbol in the list (also not valid). An almost correct way to express this is
(defmacro ( &optional (( &rest )) &rest )
...)
The extra set of parentheses removes the ambiguity. However, the definition is now incorrect because a macro call such as ( ()) would not provide any argument form for the lambda list ( &rest ), and so the default value against which to match the lambda list would be nil because no explicit default value was specified. The consequences of this are unspecified since the empty list, nil, does not have forms to satisfy the parameters and . The fully correct definition would be either
(defmacro ( &optional (( &rest ) ’(nil nil))
&rest )
...)
or
(defmacro ( &optional ((&optional &rest ))
&rest )
...)
These differ slightly: the first requires that if the macro call specifies explicitly then it must also specify explicitly, whereas the second does not have this requirement. For example,
( () ((+ 1)))
would be a valid call for the second definition but not for the first.
(
defmacro
(
&whole
) `’,
)
(
macroexpand
’(
))
(
QUOTE
(
))
(
macroexpand
’(
))
is an error.
(
defmacro
(
&whole
&optional
) `’(,
,
,
))
(
macroexpand
’(
))
is an error.
(
macroexpand
’(
))
(
QUOTE
((
)
NIL
))
(
macroexpand
’(
))
(
QUOTE
((
)
))
(
macroexpand
’(
))
is an error.
(
defmacro
(
&whole
) `’(
,
,
,
))
(
macroexpand
’(
))
(
QUOTE
(
(
)
))
(
)
(
(
)
)
(
defmacro
(
&whole
(
&whole
(
.
)
&optional
(
5
))
&body
&environment
)
``(,’,
,,
,’,
,’,(
macroexpand
)
,’,
,’,
,’,
))
; Note that because backquote is involved,
; implementations may differ slightly in the nature
; (though not the functionality) of the expansion.
(
macroexpand
’(
(((
incf
)
))
))
(
LIST*
’(
(((
INCF
)
))
)
’((((
INCF
)
))
(
SETQ
(
+
1
))
(
)
5
(
))),
T
(let (( 5))
(macrolet (( () `(cadr ,)))
( ((( ) )) )))
(( ((() )) )
5 ((() )) (CADR ) () 5 ())
SEE ALSO
define-compiler-macro(3cl), destructuring-bind(3cl), documentation(3cl), macroexpand(3cl), *macroexpand-hook*(3cl), macrolet(3cl), macro-function(3cl)
AUTHOR and COPYRIGHT
Substantial portions of this page are taken from draft proposed American National Standard for Information Systems—Programming Language—Common Lisp, X3J13/94-101R, Version 15.17R, Fri 12-Aug-1994 6:35pm EDT; no copyright indicated.
Additional clarification and comments by Michael Marking <marking@tatanka.com>, http://www.tatanka.com/software/cl-manpages/; alternatively, https://github.com/wakinyantanka/cl-manpages/. Copyright 2017 Michael Marking as both an original and a derivative work.
Licensed under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0).
This page last revised Sunday 26 February 2017.