4.11 Quasiquoting: quasiquote and `

► |  | Guide: Racket

1 | Welcome to Racket
2 | Racket Essentials
3 | Built-In Datatypes
4 | Expressions and Definitions
5 | Programmer-Defined Datatypes
6 | Modules
7 | Contracts
8 | Input and Output
9 | Regular Expressions
10 | Exceptions and Control
11 | Iterations and Comprehensions
12 | Pattern Matching
13 | Classes and Objects
14 | Units (Components)
15 | Reflection and Dynamic Evaluation
16 | Macros
17 | Creating Languages
18 | Performance
19 | Running and Creating Executables
20 | Compilation and Configuration
21 | More Libraries
22 | Dialects of Racket and Scheme
| Bibliography
| Index

▼ | 4 | Expressions and Definitions

4.1 | Notation
4.2 | Identifiers and Binding
4.3 | Function Calls (Procedure Applications)
4.4 | Functions (Procedures): lambda
4.5 | Definitions: define
4.6 | Local Binding
4.7 | Conditionals
4.8 | Sequencing
4.9 | Assignment: set!
4.10 | Quoting: quote and ’
4.11 | Quasiquoting: quasiquote and `
4.12 | Simple Dispatch: case
4.13 | Dynamic Binding: parameterize

top← prev up next →

#### 4.11`` Quasiquoting: quasiquote and `

> > > Quasiquoting: quasiquote, unquote, and unquote-splicing in Reference: Racket also documents quasiquote.

The quasiquote form is similar to quote:

> ( quasiquote datum)

However, for each ( unquote expr) that appears within the datum, the expr is evaluated to produce a value that takes the place of the unquote sub-form.

Example:
> ( quasiquote (1 2 ( unquote (+ 1 2)) ( unquote (- 5 1))))
'(1 2 3 4)

The unquote-splicing form is similar to unquote, but its expr must produce a list, and the unquote-splicing form must appear in a context that produces either a list or a vector. As the name suggests, the resulting list is spliced into the context of its use.

Example:
> ( quasiquote (1 2 ( unquote-splicing ( list (+ 1 2) (- 5 1))) 5))
'(1 2 3 4 5)

If a quasiquote form appears within an enclosing quasiquote form, then the inner quasiquote effectively cancels one layer of unquote and unquote-splicing forms, so that a second unquote or unquote-splicing is needed.

Example:

> ( quasiquote (1 2 ( quasiquote ( unquote (+ 1 2)
( unquote ( unquote (- 5 1)))))))

'(1 2 (quasiquote (unquote (+ 1 2)) (unquote 4)))

The evaluation above will not actually print as shown. Instead, the shorthand form of quasiquote and unquote will be used: ` (i.e., a backquote) and , (i.e., a comma). The same shorthands can be used in expressions:

Example:
> `(1 2 `(,(+ 1 2) ,,(- 5 1)))
'(1 2 `(,(+ 1 2) ,4))

The shorthand for of unquote-splicing is ,@:

Example:
> `(1 2 ,@( list (+ 1 2) (- 5 1)))
'(1 2 3 4)

top← prev up next →