Quotation
Quotation is a notion that is probably peculiar to Lisp. There is a special
operator, called quote
, which may be applied to any Lisp
object and the result of the evaluation is the object given as argument. This
may sound rather pointless. However, it is very important because it is the
way to differentiate between
- list data and function application.
- symbols and identifiers.
The rule to remember is that: (quote
anything)
evaluates to anything
The other useful thing to remember is that (quote
anything )
can be abbreviated by
'
anything. Quotation allows you to treat any
object as a constant, although the important case is the creation of list
constants and symbols.
- 'abc
- denotes the symbol whose name is
abc
. The quotation mark is important: if it wasn't
there, then Lisp would treat abc
as an identifier and
return the value associated with it. In the examples below, and
throughout these pages, user>
is the Lisp system's
prompt.
user> 'nil
nil
user> nil
()
user> 'ticks-per-second
ticks-per-second
user> ticks-per-second
1000.00000000000
user> '+
+
user> +
#
However, trying out the example given above has a different effect:
user> 'abc
abc
user> abc
Error---calling default handler:
Condition class is #
message: "variable unbound in module 'user'"
value: abc
Debug loop. Type help: for help
Broken at #
DEBUG> top:
user>
This is because there is no value associated with abc
:
how to do that will be discussed in the definitions page.
'(#\a "to" #(abc))
denotes the list of three
elements: the character a, the string comprising the characters t-o and
the vector containing the symbol abc
. Note that the
elements of lists do not have to be of the same type, unlike many other
languages. Again, the quotation mark is important: if it wasn't there,
then Lisp would treat '(#\a "to" #(abc))
as the
application of the character #\a
to the
arguments "to"
and #(abc)
. For
example:
user> '(+ 1 2)
(+ 1 2)
user> (+ 1 2)
3
However, trying out the example given above has this result:
user> '(#\a "to" #(abc))
(#\a "to" #\z)
user> (#\a "to" #(abc))
Error---calling default handler:
Condition class is #
message: "incorrect type in function application"
value: #\a
expected-type: #
Debug loop. Type help: for help
Broken at #
DEBUG> top:
user>
A fuller explanation of function application is given in the expressions page.
Quotation and constants
Now that enough has been explained about quotation and constants, it is time
to discuss how they interact, in particular with respect to vectors, since these
are the only constants which can contain arbitrary lisp objects. This is
probably best done by example:
user> #("hi" '(+ 1 2) 'abc)
#("hi" (quote (+ 1 2)) (quote abc))
user> #("hi" (+ 1 2) abc)
#("hi" (+ 1 2) abc)
user> '("hi" (+ 1 2) abc)
("hi" (+ 1 2) abc)
user> ("hi" (+ 1 2) abc)
Error---calling default handler:
Condition class is #
message: "variable unbound in module 'user'"
value: abc
Debug loop. Type help: for help
Broken at #
DEBUG> top:
user>
From the first example, we see that the quoted expression appears with
the quote in the resulting vector; that is, the elements of vector
constant are not evaluated, as if there was a quotation outside the
vector. The second example simply demonstrates what we should expect,
given the first example. The third is a quoted list, so of course,
the elements do not get evaluated either. Predictably, if we leave
off the quote from the list, then it treats the expression as a
function application and tried to evaluate the elements before
applying the first one. Because abc
has no value (in
Lisp, we say it is unbound), we get the same error as we saw
before.
Julian Padget, jap@maths.bath.ac.uk, this version December 9, 1994