Next: , Previous: , Up: Macros   [Contents][Index]

5.10.2 Backquoting

As seen in the previous sections, macros are a very powerful mechanism of defining new control structures. However due to the need to create the expansion, i.e. the form that will be actually evaluated, they can often be complex to write and understand.

We have already seen that constants may be produced through the use of the quote-mark (see Quoting), here another form of quoting is described, where only some of the quoted object is actually constant. This is known as backquoting, since it is introduced by the backquote character ‘`’, a shortcut for the backquote macro.

Macro: backquote arg

Constructs a new version of arg (a list). All parts of list are preserved except for expressions introduced by comma (‘,’) characters, which are evaluated and spliced into the list. For example:

`(1 2 ,(+ 1 2))
    ⇒ (1 2 3)

Also, the ‘,@’ prefix will splice the following list into the output list, at the same level:

`(1 2 ,@(list 3))
    ⇒ (1 2 3)

Backquoting allows macros expansions to be created from static templates. For example the when macro shown in the previous sections can be rewritten as:

(defmacro when (condition #!rest body)
  `(cond (,condition ,@body)))

which is easier to read, since it is a lot closer to the actual expansion.