3.20 Quasiquoting: quasiquote, unquote, and unquote-splicing

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 3 | Syntactic Forms

3.1 | Modules: module, module*, ...
3.2 | Importing and Exporting: require and provide
3.3 | Literals: quote and #%datum
3.4 | Expression Wrapper: #%expression
3.5 | Variable References and #%top
3.6 | Locations: #%variable-reference
3.7 | Procedure Applications and #%app
3.8 | Procedure Expressions: lambda and case-lambda
3.9 | Local Binding: let, let*, letrec, ...
3.10 | Local Definitions: local
3.11 | Constructing Graphs: shared
3.12 | Conditionals: if, cond, and, and or
3.13 | Dispatch: case
3.14 | Definitions: define, define-syntax, ...
3.15 | Sequencing: begin, begin0, and begin-for-syntax
3.16 | Guarded Evaluation: when and unless
3.17 | Assignment: set! and set!-values
3.18 | Iterations and Comprehensions: for, for/ list, ...
3.19 | Continuation Marks: with-continuation-mark
3.20 | Quasiquoting: quasiquote, unquote, and unquote-splicing
3.21 | Syntax Quoting: quote-syntax
3.22 | Interaction Wrapper: #%top-interaction
3.23 | Blocks: block
3.24 | Internal-Definition Limiting: #%stratified-body
3.25 | Performance Hints: begin-encourage-inline
3.26 | Importing Modules Lazily: lazy-require

On this page:

quasiquote
unquote
unquote-splicing

top contents ← prev up next →

#### 3.20`` Quasiquoting: quasiquote, unquote, and unquote-splicing🔗ℹ

> > > Quasiquoting: quasiquote and ‘ in The Racket Guide introduces quasiquote.

> syntax (quasiquote datum)

The same as 'datum if datum does not include (unquote expr) or (unquote-splicing expr). An (unquote expr) form escapes from the quote, however, and the result of the expr takes the place of the (unquote expr) form in the quasiquote result. An (unquote-splicing expr) similarly escapes, but the expr produces a list whose elements are spliced as multiple values place of the (unquote-splicing expr).

An unquote or unquote-splicing form is recognized in any of the following escaping positions within datum: in a pair, in a vector, in a box, in a prefab structure field after the name position, and in hash table value position (but not in a hash table key position). Such escaping positions can be nested to an arbitrary depth.

An unquote-splicing form must appear as the car of 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 an escaping position but in a way other than as (unquote expr) or (unquote-splicing 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: contract violation
> expected: 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)
> > `#hash(("a" . ,(+ 1 2)))
> '#hash(("a" . 3))
> > `#hash((,(+ 1 2) . "a"))
> '#hash((,(+ 1 2) . "a"))
> > `(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. When allocating fresh data, the quasiquote form allocates mutable vectors, mutable boxes and immutable hashes.

Examples:

> > (immutable? `#(,0))
> #f
> > (immutable? `#hash((a . ,0)))
> #t

> syntax unquote

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

> syntax unquote-splicing

See quasiquote, where unquote-splicing is recognized as an escape. An unquote-splicing form as an expression is a syntax error.
top contents ← prev up next →