7
votes

While writing a function in Racket I accidently put two single quotes in front of a symbol instead of one. i.e. I accidently wrote ''a and discovered some behaviour of nested quotes that seems strange. I'm using DrRacket and tested this with both the Racket lang and the R5RS lang.

(write (pair? (quote (quote a))))

prints: #t .

(write (car (quote (quote a))))

prints: quote

But

(write (quote (quote a)))

and

(write '(quote a)))

Both print: 'a

Can someone tell me why in Scheme (and Racket) the function pair? interprets (quote (quote a))) as a pair of two elements quote and a , but the function write prints out 'a instead of (quote a) .

1

1 Answers

8
votes

Putting a quote mark (') around a term and wrapping a quote form around it are identical. That is, they read to the same term.

So all of the following expressions are identical in Scheme:

''a
'(quote a)
(quote 'a)
(quote (quote a))

The quote form means "interpret what comes next as a datum---even if it contains another quote". The sub-term is parenthesized, so it's a list; the inner quote is just a symbol.

In some cases, the printer uses reader-abbreviations like the quote mark (') in its output. I'm a little surprised that you got write to do it, though; for me, it always writes as (quote a).