1 Language Model
2 Syntactic Forms
3 Datatypes
4 Structures
5 Classes and Objects
6 Units
7 Contracts
8 Pattern Matching
9 Control Flow
10 Concurrency
11 Macros
12 Input and Output
13 Reflection and Security
14 Operating System
15 Memory Management
16 Running PLT Scheme
Bibliography
Index
On this page:
quasiquote
unquote
unquote-splicing
Version: 4.0.2

 

2.20 Quasiquoting: quasiquote, unquote, and unquote-splicing

(quasiquote datum)

The same as 'datum if datum does not include ,expr or ,@expr. An ,expr expression escapes from the quote, however, and the result of the expr takes the place of the ,expr form in the quasiquote result. An ,@expr similarly escapes, but the expr must produce a list, and its elements are spliced as multiple values place of the ,@expr, which must appear as the car or a quoted pair, as an element of a quoted vector, or as an element of a quoted prefab structure; in the case of a pair, if the cdr of the relevant quoted pair is empty, then expr need not produce a list, and its result is used directly in place of the quoted pair (in the same way that append accepts a non-list final argument). If unquote or unquote-splicing appears within quasiquote in any other way than as ,expr or ,@expr, a syntax error is reported.

Examples:

  > (quasiquote (0 1 2))

  (0 1 2)

  > (quasiquote (0 (unquote (+ 1 2)) 4))

  (0 3 4)

  > (quasiquote (0 (unquote-splicing (list 1 2)) 4))

  (0 1 2 4)

  > (quasiquote (0 (unquote-splicing 1) 4))

  unquote-splicing: expected argument of type <proper list>;

  given 1

  > (quasiquote (0 (unquote-splicing 1)))

  (0 . 1)

A quasiquote, unquote, or unquote-splicing form is typically abbreviated with `, ,, or ,@, respectively. See also Reading Quotes.

Examples:

  > `(0 1 2)

  (0 1 2)

  > `(1 ,(+ 1 2) 4)

  (1 3 4)

  > `#s(stuff 1 ,(+ 1 2) 4)

  #s(stuff 1 3 4)

  > `(1 ,@(list 1 2) 4)

  (1 1 2 4)

  > `#(1 ,@(list 1 2) 4)

  #(1 1 2 4)

A quasiquote form within the original datum increments the level of quasiquotation: within the quasiquote form, each unquote or unquote-splicing is preserved, but a further nested unquote or unquote-splicing escapes. Multiple nestings of quasiquote require multiple nestings of unquote or unquote-splicing to escape.

Examples:

  > `(1 `,(+ 1 ,(+ 2 3)) 4)

  (1 `,(+ 1 5)  4)

  > `(1 ```,,@,,@(list (+ 1 2)) 4)

  (1 ```,,@,3    4)

The quasiquote form allocates only as many fresh cons cells, vectors, and boxes as are needed without analyzing unquote and unquote-splicing expressions. For example, in

  `(,1 2 3)

a single tail '(2 3) is used for every evaluation of the quasiquote expression.

unquote

See quasiquote, where unquote is recognized as an escape. An unquote form as an expression is a syntax error.

unquote-splicing

See quasiquote, where unquote-splicing is recognized as an escape. An unquote-splicing form as an expression is a syntax error.