[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Backquote proposal per issue 99

Here is the backquote proposal per issue 99.  It is exactly
what is in the latest Common LISP Manual draft, except that
I have conquered the SCRIBE bug that caused backquotes not
to print.

`  The backquote (accent grave) character makes it easier to write
   programs to construct complex data structures by using a template.
   As an example, writing

       `(cond ((numberp ,x) ,@y) (t (print ,x) ,@y))

   is roughly equivalent to writing

       (list 'cond
             (cons (list 'numberp x) y)
             (list* 't (list 'print x) y))

   The general idea is that the backquote is followed by a template, a
   picture of a data structure to be built.  This template is copied,
   except that within the template commas can appear.  Where a comma
   occurs, the form following the comma is to be evaluated to produce an
   object to be inserted at that point.  Assume B has the value 3, for
   example, then evaluating the form denoted by ```(A B ,B ,(+ B 1) B)''
   produces the result (A B 3 4 B).
   If a comma is immediately followed by an at-sign (``@''), then the
   form following the at-sign is evaluated to produce a list of objects.
   These objects are then ``spliced'' into place in the template.  For
   example, if X has the value (A B C), then

       `(x ,x ,@x foo ,(cadr x) bar ,(cdr x) baz ,@(cdr x))
          -> (x (a b c) a b c foo b bar (b c) baz b c)

   The backquote syntax can be summarized formally as follows.  For each
   of several situations in which backquote can be used, a possible
   interpretation of that situation as an equivalent form is given.
   Note that the form is equivalent only in the sense that when it is
   evaluated it will calculate the correct result.  An implementation is
   quite free to interpret backquote in any way such that a backquoted
   form, when evaluated, will produce a result EQUAL to that produced by
   the interpretation shown here.

      - `simple is the same as 'simple, that is, (QUOTE simple),
        for any form simple that is not a list or a general vector.

      - `,form is the same as form, for any form, provided that the
        representation of form does not begin with ``@'' or ``.''.
        (A similar caveat holds for all occurrences of a form after
        a comma.)

      - `,@form is an error.

      - `(x1 x2 x3 ... xn . atom) may be interpreted to mean
        (APPEND x1 x2 x3 ... xn (QUOTE atom)), where the underscore
        indicates a transformation of an xj as follows:

           * form is interpreted as (LIST `form), which contains a
             backquoted form that must then be further interpreted.

           * ,form is interpreted as (LIST form).

           * ,@form is interpreted simply as form.

      - `(x1 x2 x3 ... xn) may be interpreted to mean the same as
        `(x1 x2 x3 ... xn . NIL).

      - `(x1 x2 x3 ... xn . ,form) may be interpreted to mean
        (APPEND x1 x2 x3 ... xn form), where the underscore
        indicates a transformation of an xj as above.

      - `(x1 x2 x3 ... xn . ,@form) is an error.

      - `#(x1 x2 x3 ... xn) may be interpreted to mean (MAKE-VECTOR
        NIL :INITIAL-CONTENTS `(x1 x2 x3 ... xn)).

   No other uses of comma are permitted; in particular, it may not
   appear within the #A or #S syntax.
   Anywhere ``,@'' may be used, the syntax ``,.'' may be used instead to
   indicate that it is permissible to destroy the list produced by the
   form following the ``,.''; this may permit more efficient code, using
   NCONC instead of APPEND, for example.
   If the backquote syntax is nested, the innermost backquoted form
   should be expanded first.  This means that if several commas occur in
   a row, the leftmost one belongs to the innermost backquote.
   Once again, it is emphasized that an implementation is free to
   interpret a backquoted form as any form that, when evaluated, will
   produce a result that is EQUAL to the result implied by the above
   definition.  In particular, no guarantees are made as to whether the
   constructed copy of the template will or will not share list
   structure with the template itself.  As an example, the above
   definition implies that `((,A B) ,C ,@D) will be interpreted as if it

       (append (list (append (list a) (list 'b) 'NIL)) (list c) d 'NIL)

   but it could also be legitimately interpreted to mean any of the

       (append (list (append (list a) (list 'b))) (list c) d)
       (append (list (append (list a) '(b))) (list c) d)
       (append (list (cons a '(b))) (list c) d)
       (list* (cons a '(b)) c d)
       (list* (cons a (list 'b)) c d)
       (list* (cons a '(b)) c (copylist d))

   (There is no good reason why COPYLIST should be performed, but it is
   not prohibited.)