Suppose I know at the time I write a program what values I want to put
into a pair. Is there some way for me simply to refer to that pair, the
way I can use the numeral 5 to refer to the integer five or
the literal #t to refer to the true Boolean value? Or do I
have to call the cons procedure to build it?
Scheme provides a syntax for literals that refer to pairs: Take a literal
for each component of the pair, place a dot between them, enclose the lot
in parentheses, and finally attach a single quotation mark at the
beginning. So, for instance, the literal for a pair containing the
integers 59 and 117 is '(59 . 117).
Except for the single quotation mark, it's the same format that the interpreter uses to print out pair values.
That's right. In fact, some interpreters (such as Scheme48) will even attach the single quotation mark when displaying pair values:
Others, like the SCM interpreter that I've used for most examples, omit the single quotation mark on output:> (cons 59 117) '(59 . 117)
It would be a good idea to find out which convention your local interpreter uses.> (cons 59 117) (59 . 117)
What's the point of the single quotation mark, anyway? Why can't the programmer just type in the same thing that the SCM interpreter prints out?
Because the Scheme processor will mistake it for a procedure call:
When it sees a left parenthesis at the beginning of an expression to be evaluated, Scheme expects to find immediately after it either one of its keywords (such as> (59 . 117) ERROR: Wrong type to apply: (see errobj) ; in expression: (... 59 . 117) ; in top level environment.
if, lambda, or
let) or an expression that denotes a procedure to be called.
The expression 59 is neither of these things, so Scheme
complains that it can't call an integer. The single quotation mark informs the Scheme processor that what follows it is a datum -- not an expression to be evaluated, but a direct literal representation of a value:
Scheme turns off its evaluator while processing a quoted literal and just builds the pair that it describes.> '(59 . 117) (59 . 117)
In fact, you can attach a single quotation mark to a numeral or a Boolean literal if you like, though it will have no effect:
The reason is that numerals and Boolean literals are already recognized as data, so that explicitly marking them as data doesn't affect the way the Scheme processor deals with them.> 5 5 > '5 5 > (+ '5 '7) 12
What happens if you put a single quotation mark in front of some expression that isn't a datum?
The quotation mark turns the expression into a datum and prevents its evaluation:
In the first two cases shown here, the value of the quoted literal is a symbol; the symbol data type will be discussed later on. In the other three cases, the value is a list; lists constitute a sub-type of pairs, as we'll see shortly. For now, the only point is that an expression such as> 'add1 add1 > 'lambda lambda > '(add1 5) (add1 5) > '(lambda (x) (+ x 1)) (lambda (x) (+ x 1)) > '(if lambda) (if lambda)
(add1 5) is not evaluated or reduced to the integer 6 when it
is protected by the single quotation mark -- the quotation mark suppresses
evaluation.
Next topic
Previous topic
Table of contents
This document is available on the World Wide Web as
http://www.math.grin.edu/~stone/scheme-web/pair-literals.html