1 Welcome to PLT Scheme
2 Scheme 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 Performance
18 Running and Creating Executables
19 Compilation and Configuration
20 More Libraries
Bibliography
Index
Version: 4.0.2

 

4.11 Quasiquoting: quasiquote and `

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.

Examples:

  > (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.

Examples:

  > (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.

Examples:

  > (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:

Examples:

  > `(1 2 `(,(+ 1 2) ,,(- 5 1)))

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

The shorthand for of unquote-splicing is ,@:

Examples:

  > `(1 2 ,@(list (+ 1 2) (- 5 1)))

  (1 2 3 4)