r7rs-small-texinfo

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/r7rs-small-texinfo
Log | Files | Refs | README | LICENSE

quasiquotation.texinfo (3802B)


      1 @node Quasiquotation
      2 @subsection Quasiquotation
      3 
      4 @cindex backquote
      5 @cindex comma
      6 
      7 @c FIXME: deffn doesn't handle the ` line correctly.
      8 @deffn  syntax quasiquote @svar{qq template}
      9 @deffnx syntax `@svar{qq template}
     10 @deffnx {auxiliary syntax} unquote
     11 @deffnx {auxiliary syntax} @comma{}
     12 @deffnx {auxiliary syntax} unquote-splicing
     13 @deffnx {auxiliary syntax} @comma{}@@
     14 
     15 ``Quasiquote'' expressions are useful
     16 for constructing a list or vector structure when some but not all of the
     17 desired structure is known in advance. If no
     18 commas appear within the @svar{qq template}, the result of
     19 evaluating
     20 @code{`}@svar{qq template} is equivalent to the result of evaluating
     21 @code{'}@svar{qq template}. If a comma appears within the
     22 @svar{qq template}, however, the expression following the comma is
     23 evaluated (``unquoted'') and its result is inserted into the structure
     24 instead of the comma and the expression. If a comma appears followed
     25 without intervening whitespace by a commercial at-sign (@@), then it is an error if the following
     26 expression does not evaluate to a list; the opening and closing parentheses
     27 of the list are then ``stripped away'' and the elements of the list are
     28 inserted in place of the comma at-sign expression sequence. A comma
     29 at-sign normally appears only within a list or vector @svar{qq template}.
     30 
     31 Note: In order to unquote an identifier beginning with @code{,} it is necessary to use either
     32 an explicit unquote or to put whitespace after the comma, to avoid colliding with the
     33 comma at-sign sequence.
     34 
     35 @lisp
     36 `(list ,(+ 1 2) 4) @result{} (list 3 4)
     37 
     38 (let ((name 'a)) `(list ,name ',name))
     39   @result{} (list a (quote a))
     40 
     41 `(a ,(+ 1 2) ,@@(map abs '(4 -5 6)) b)
     42   @result{}  (a 3 4 5 6 b)
     43 
     44 `(( foo ,(- 10 3)) ,@@(cdr '(c)) . ,(car '(cons)))
     45   @result{} ((foo 7) . cons)
     46 
     47 `#(10 5 ,(sqrt 4) ,@@(map sqrt '(16 9)) 8)
     48   @result{} #(10 5 2 4 3 8)
     49 
     50 (let ((foo '(foo bar)) (@@baz 'baz))
     51   `(list ,@@foo , @@baz))
     52   @result{} (list foo bar baz)
     53 @end lisp
     54 
     55 Quasiquote expressions can be nested. Substitutions are made only
     56 for unquoted components appearing at the same nesting level as the outermost
     57 quasiquote. The nesting level increases by one inside each successive quasiquotation, and
     58 decreases by one inside each unquotation.
     59 
     60 @lisp
     61 `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
     62   @result{}  (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
     63 
     64 (let ((name1 'x)
     65       (name2 'y))
     66   `(a `(b ,,name1 ,',name2 d) e))
     67   @result{}  (a `(b ,x ,'y d) e)
     68 @end lisp
     69 
     70 A quasiquote expression may return either newly allocated, mutable
     71 objects or literal structure for any structure that is constructed at run time during the
     72 evaluation of the expression. Portions that do not need to be rebuilt are always literal.
     73 Thus, @code{(let ((a 3)) `((1 2) ,a ,4 ,'five 6))} may be treated as equivalent to either of the following
     74 expressions:
     75 
     76 @lisp
     77 `((1 2) 3 4 five 6)
     78 
     79 (let ((a 3))
     80   (cons '(1 2)
     81         (cons a (cons 4 (cons 'five '(6))))))
     82 @end lisp
     83 
     84 However, it is not equivalent to this expression:
     85 
     86 @lisp
     87 (let ((a 3)) (list (list 1 2) a 4 'five 6))
     88 @end lisp
     89 
     90 The two notations @code{`}@svar{qq template} and
     91 @code{(quasiquote }@svar{qq template}@code{)}
     92 are identical in all respects. @code{,}@svar{expression} is identical to
     93 @code{(unquote }@svar{expression}@code{)}, and @code{,@@}@svar{expression}
     94 is identical to @code{(unquote-splicing }@svar{expression}@code{)}. The
     95 write procedure may output either format.
     96 
     97 @lisp
     98 (quasiquote (list (unquote (+ 1 2)) 4))
     99   @result{} (list 3 4)
    100 
    101 '(quasiquote (list (unquote (+ 1 2)) 4))
    102   @result{} `(list ,(+ 1 2) 4)
    103   @r{i.e.}, (quasiquote (list (unquote (+ 1 2)) 4))
    104 @end lisp
    105 
    106 It is an error if any of the identifiers @code{quasiquote}, @code{unquote},
    107 or @code{unquote-splicing} appear in positions within a @svar{qq template}
    108 otherwise than as described above.
    109 
    110 @end deffn