next up previous contents
Next: Procedures Up: Scheme Tutorial Previous: Logical composition operators   Contents

Subsections

Quote and quasiquote

Quote

Quotation is used when literal constants need to be included in the code. This is done by using quote, which has the following syntax:

(quote datum)

The expression evaluates to datum, which may be any external representation of a Scheme object, i.e. a sequence of characters. For example, the external representation of the integer 10 is the sequence of characters ``10''. It is also possible to abbreviate the expression above in the following way:

'datum

The two notations are equivalent. Let us take a look at some examples:

> (quote a)
a
> 'a
a
> (quote foo)
foo
> (quote (quote foo))
(quote foo)
> (quote 'foo)
(quote foo)
> ''foo
(quote foo)

The difference between a quoted and an unquoted symbol is the following:

> (define a 10)
> a
10
> 'a
a

As you can see, when a was left unquoted when fed to the interpreter, it was evaluated and its value was returned. When we quoted a we were not interested in the value of the variable a but rather in the symbol a.

Let's say we have the list (a 1 2) and we feed it to the interpreter. What happens? Well, the interpreter will try to apply the arguments 1 and 2 to the procedure a. To prevent this from happening, we simply quote the list:

> (a 1 2)
procedure application: expected procedure, given: 10; arguments were: 1 2
> '(a 1 2)
(a 1 2)

Quasiquote

Quasiquotes are used to construct lists or vectors when some of the desired structure is not known in advance. The syntax of quasiquote is the following:

(quasiquote <qq template>)

It is also possible to use the abbreviated but equivalent notation:

`<qq template>

If no commas appear within <qq template>, the result is the same as for evaluating '<qq template>:

> `(1 2)
(1 2)
> '(1 2)
(1 2)

However, if a comma appears, the expression which follows the comma will be evaluated and the value returned will be inserted into the structure in place of the comma expression, for example:

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

The comma can also be expressed using unquote. The notations are equivalent.

> `(1 (unquote (+ 2 3)) 4)
(1 5 4)

If the comma is followed by an @ (at-sign), then the expression that follows must evaluate to a list, for example:

> `(,@(cdr '(1 2 3)))
(2 3)
> `(1 ,(acos -1) ,@(map sqrt '(1 4 9 16 25)))
(1 3.1415926535898 1 2 3 4 5)
> (define ls '(2 3))
> `(1 ,ls 4)
(1 (2 3) 4)
> `(1 ,@ls 4)
(1 2 3 4)

As can be seen from the examples above, when a comma is used, the expression following it will be evaluated and the value returned will replace the whole comma expression. If @ is used, the elements of the list that is returned replace the @ expression.

It is also possible to use unquote-splicing instead of @. The two notations are equivalent:

> `(,@(cdr '(1 2 3)))
(2 3)
> `((unquote-splicing (cdr '(1 2 3))))
(2 3)

next up previous contents
Next: Procedures Up: Scheme Tutorial Previous: Logical composition operators   Contents
Timo Lilja 2001-09-11