(lispref.info)Backquote


Next: Problems with Macros Prev: Defining Macros Up: Macros

Backquote
=========

   It could prove rather awkward to write macros of significant size,
simply due to the number of times the function `list' needs to be
called.  To make writing these forms easier, a macro ``' (often called
"backquote") exists.

   Backquote allows you to quote a list, but selectively evaluate
elements of that list.  In the simplest case, it is identical to the
special form `quote' (Note: Quoting.).  For example, these two forms
yield identical results:

     (` (a list of (+ 2 3) elements))
          => (a list of (+ 2 3) elements)
     (quote (a list of (+ 2 3) elements))
          => (a list of (+ 2 3) elements)

   By inserting a special marker, `,', inside of the argument to
backquote, it is possible to evaluate desired portions of the argument:

     (list 'a 'list 'of (+ 2 3) 'elements)
          => (a list of 5 elements)
     (` (a list of (, (+ 2 3)) elements))
          => (a list of 5 elements)

   It is also possible to have an evaluated list "spliced" into the
resulting list by using the special marker `,@'.  The elements of the
spliced list become elements at the same level as the other elements of
the resulting list.  The equivalent code without using ``' is often
unreadable.  Here are some examples:

     (setq some-list '(2 3))
          => (2 3)
     (cons 1 (append some-list '(4) some-list))
          => (1 2 3 4 2 3)
     (` (1 (,@ some-list) 4 (,@ some-list)))
          => (1 2 3 4 2 3)
     
     (setq list '(hack foo bar))
          => (hack foo bar)
     (cons 'use
       (cons 'the
         (cons 'words (append (cdr list) '(as elements)))))
          => (use the words foo bar as elements)
     (` (use the words (,@ (cdr list)) as elements (,@ nil)))
          => (use the words foo bar as elements)

   The reason for `(,@ nil)' is to avoid a bug in Emacs version 18.
The bug occurs when a call to `,@' is followed only by constant
elements.  Thus,

     (` (use the words (,@ (cdr list)) as elements))

would not work, though it really ought to.  `(,@ nil)' avoids the
problem by being a nonconstant element that does not affect the result.

 - Macro: ` LIST
     This macro returns LIST as `quote' would, except that the list is
     copied each time this expression is evaluated, and any sublist of
     the form `(, SUBEXP)' is replaced by the value of SUBEXP.  Any
     sublist of the form `(,@ LISTEXP)' is replaced by evaluating
     LISTEXP and splicing its elements into the containing list in
     place of this sublist.  (A single sublist can in this way be
     replaced by any number of new elements in the containing list.)

     There are certain contexts in which `,' would not be recognized and
     should not be used:

          ;; Use of a `,' expression as the CDR of a list.
          (` (a . (, 1)))                             ; Not `(a . 1)'
               => (a \, 1)

          ;; Use of `,' in a vector.
          (` [a (, 1) c])                             ; Not `[a 1 c]'
               error--> Wrong type argument

          ;; Use of a `,' as the entire argument of ``'.
          (` (, 2))                                   ; Not 2
               => (\, 2)

     Common Lisp note: in Common Lisp, `,' and `,@' are implemented as
     reader macros, so they do not require parentheses.  Emacs Lisp
     implements them as functions because reader macros are not
     supported (to save space).


automatically generated by info2www