Macros are different from functions in that a macro can be called with any number of arguments. Defmacro can handle this and can be used in the same way as defun:
- If there is an explicit list of variables after the macro name then it expects exactly that number of arguments and otherwise generates an error.
(defmacro do3 (a b c) `(progn ,a ,b ,c))
- Defmacro can also take clauses like defun where each clause has one argument which is a list of the arguments to the macro call. This is one way the macro handles any number of arguments.
(defmacro list*
((list e) e) ;A list of one element (cons e ())
((cons e es) `(cons ,e (list* . ,es)))
(() ()))
- But defmacro can also take just a variable, not a list of variables, in which case the variable will be the of arguments to the macro call. This is another way the macro handles any number of arguments
(defmacro list* args
(case args
((list e) e) ;A list of one element (cons e ())
((cons e es) `(cons ,e (list* . ,es)))
(() ())))
The last two are equivalent, you can use the one that feels the best. Normally replacing matching using clauses with a case becomes messier if there are many arguments but here there is always only one.
Robert