defmacro is a function for defining macros in Lisp and Scheme similar to defun, which defines functions; however, defmacro never evaluates its arguments, which allows one to perform complex manipulation of code. A simple example:

(defmacro iff (Test Then &optional Else)
  "A replacement for IF, takes 2 or 3 arguments. If the 
first evaluates to non-NIL, evaluate and return the second.
Otherwise evaluate and return the third (which defaults to NIL)"
     (,Test ,Then)
     (t     ,Else)))

Thus, calling (iff (= a 7) do-something (1+ a)) results in the macro being expanded to:

    ((= a 7) do-something)
    (t       (1+ a)))
The code is literally transformed by the macro rather than evaluated. Other, more complex, transformations are possible as well. Much of the Common Lisp standard is implemented with macros. This metaprogrammability is one of the features that gives Lisp its power and flexibility.

Log in or register to write something here or to contact authors.