Scheme allows you to write lists as literals using quoting.
Just as you can write a literal boolean or number in your program,
you can write a literal list if you use the special form quote
.
Quote is a special form, not a procedure, because it doesn't evaluate its argument in the usual way. (Its argument is really just a literal representation of a data structure, which may look like a Scheme expression, but it isn't.)
For example, the expression (quote (1 2 3))
returns a pointer
to a list (1 2 3)
, i.e., a sequence of cdr linked pairs whose
car values are (pointers to) to 1, 2, and 3.
You can use quote
expressions as subexpressions of other
expressions, because they just return pointer values like anything
else.
For example, the expression (define foo (quote (1 2 3)))
defines (and binds) a variable foo
, and initializes its binding
with (a pointer to) a three-element list.
We can draw the resulting situation this way:
+---+ +---+---+ +---+---+ +---+---+ foo | *-+--->| * | *-+----->| * | *-+----->| * | * | +---+ +-+-+---+ +-+-+---+ +-+-+---+ | | | \|/ \|/ \|/ 1 2 3
quote
takes exactly one argument, and returns a data structure
whose printed representation is the same as what you typed in as the
argument to quote
. Scheme does not evaluate the argument
to quote
as an expression--it just gives you a pointer to a
data structure.
Note that quote
does not generally construct a character string--it
constructs a data structure that may be a list or tree or even an
array. It's a very general quoting facility, much more powerful than
the double quotes around character strings, which only construct string
objects.
Scheme provides a cleaner way of writing quote
d expressions,
using the special single-quote character '
. Rather than writing
out (quote
some-expression)
, you can just precede
the quoted expression with the single-quote character. For example,
we can write the same definition of foo
as
(define foo '(1 2 3))
. You don't need a closing quote, because
of Scheme's parenthesized prefix syntax--it can figure out where the
quoted data structure ends.
One subtlety about quote
is that a quote
expression
doesn't create a data structure every time it's called--evaluating
the same expression many times may return many pointers to the
same structure.
Consider the procedure definition
(define (foo) '(1 2 3))
The list (1 2 3)
may be created when we define the
procedure foo
, and each time we call it, it may return a
pointer to that same list. (Exactly what happens depends on the
particular implementation of Scheme, but most work this way, for
efficiency reasons. Evaluating the quote
expression just
fetches a pointer to a data structure that was created beforehand.)
For this reason, it's an error to modify a data structure returned
from a quote
form. Unfortunately, many Scheme systems don't
detect this error, and will let you do it. If you want a new data
structure each time, you should use a procedure like list
, which
always creates a new data structure. (list
, which we'll
discuss more later, is a standard Scheme procedure that takes any
number of arguments, and creates a list of those items.)
For example, if we want the
procedure foo
to return a new list (1 2 3)
every time, we
can write
(define (foo) (list 1 2 3))