2

This question arose when reading SICP. Why (list 'quote '(a b c)) evaluated by the interpreter (R5RS in Dr.Racket) as '(a b c). For me it should be (quote (a b c)). For instance (list 'quot '(a b c)) is evaluated as (quot (a b c)). What is so special in the 'quote?

mosceo
  • 1,222
  • 11
  • 26
  • What interpreter are you using? – Óscar López Dec 17 '13 at 19:49
  • Since the other answers are essentially duplicates, I'll add this here, to add to what they say: Even when you are looking at the single-quote syntax, what you are really seeing is a non-empty ***list***: `(consp ''42)` is true (returns non-`nil`). – Drew Dec 17 '13 at 20:15

4 Answers4

6

You'll get different behaviors depending on exactly what Lisp you're using (Scheme, Racket, Common Lisp, etc.) but in general, the system will accept 'x as a shorthand or syntactic sugar for (quote x). The two forms are exactly equivalent and their values are the same: the unevaluated x. When a result is coming out of the system, it might choose to print in the first way to make the result more intuitive to the user. A similar thing happens with cons, too. For instance,

(cons 1 2)
;=> (1 . 2)

because that's the general way that cons cells (pairs) are printed. However, there's a special case defined for when the second part of the pair is another list (either the empty list () or another pair, and that's why we have the following. I've also written a bit more about how lists and cons cells are printed in an answer to Recursive range in Lisp adds a period?.

(cons 1 '())
;=> (1)

(cons 1 '(2 3))
;=> (1 2 3)

Now, I've written the values of the expression above. E.g., the value of the form (cons 1 '(2 3)) is the list (1 2 3). As an additional complication, some systems (I'm thinking of some languages is Dr. Racket, in particular) don't print the value of a form in the interactive prompt, but rather print a form that would produce the same (for certain interpretations of “the same”) values. For instance, you might evaluate '(1 . 2) and see the output (cons 1 2) because that's another form that would produce the same value. This can be helpful if you're doing pure functional programming that has referential transparency, but if you're not expecting it, it can lead to some confusion.

A good way to see that we're getting the results that we should, regardless of how the system prints them, is to inspect them. We expect that (list 'quote '(a b c)) should return a list whose car is the symbol quote and whose cadr is the list (a b c). This is what we get (in Dr. Racket 5.3 with language R5RS):

> (display (list 'quote '(a b c)))
'(a b c)
> (display (car (list 'quote '(a b c))))
quote
> (display (cadr (list 'quote '(a b c))))
(a b c)

We get similar results if we use 'qmmmt instead of 'quote:

> (display (list 'qmmmt '(a b c)))
(qmmmt (a b c))
> (display (car (list 'qmmmt '(a b c))))
qmmmt
> (display (cadr (list 'qmmmt '(a b c))))
(a b c)

The only difference is that in the first case, display displays the list whose car is the symbol quote using the shorthand that is available for such lists. That is, instead of displaying (quote (a b c)) it displayed '(a b c).

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
4

'(a b c) and (quote (a b c)) are actually different notations for the same. So don't be surprised if your Lisp prints the shorter version.

In general '<something> is the same as (quote <something>).

QUOTE is used in Lisp to mark expressions which should evaluate to themselves. Usually a list would be a function or macro call and a symbol would be a variable. If you want to treat those as data, you need to quote them.

Since (quote <something>) is used so often in Lisp, the abbreviated version '<something> has been introduced to save a bit of typing or reading...

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • I want to emphasize that in the question I'm using `'quote` not `quote`. And `'quote` is represented differently, not like `'qmmmt` for instance. – mosceo Dec 17 '13 at 21:11
  • For example `(list 'quote '(a b c))` is represented as `'(a b c)`, `(list 'c 'quote '(a b c))` is represented as `(c quote (a b c))`, `(list 'quo '(a b c))` is represented as `(quo (a b c))` – mosceo Dec 17 '13 at 21:14
  • @Graduate the system may _print_ the list `(quote abc)` and the list `(qmmmt abc)` differently (as `'abc` and `(qmmmt abc)`) respectively, but both are lists; if you call `first` and `second` on them, you'll see the expected values. This is just a matter of _printing_. – Joshua Taylor Dec 17 '13 at 21:27
  • I was just interested why it prints differently. – mosceo Dec 17 '13 at 21:38
2

display emits some behaviors. Eg. '(a . (b . (c . ()))) is displayed (a b c). 'quote is displayed quote and perhaps '(quote x y) is displayed (quote x y) while '(quote x) is displayed 'x or (quote x). Which one is implementation dependent but both mean the same.

As data (ie. quoted, like (quote quote) and it's abbrivation 'quote) the result of the evaluation, the symbol quote is nothing special, for any LISP, just like '+ and 'potato happen to be the symbols + and potato. Any symbol that mean something when not quoted is no special when quoted.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • 2
    It's very important to make it clear whether you're putting _values_ or _forms_ in your code blocks. When you say that the `(quote quote)` is abbreviated `'quote`, you're talking about the value that is a list of length two and each of whose elements are the symbol `quote`. When you say that `'+` and `'potato` are symbols, though, you mean that the _forms_ `'+` and `'potato`, when evaluated, produce the values that are the symbol `+` and the symbol `potato`. Usually the meaning is clear from context, but this is a question _about_ what things mean in different contexts. – Joshua Taylor Dec 17 '13 at 21:25
  • @JoshuaTaylor With `(quote quote)` I was thinking as a form.. eg. quote being bound to a special form and that it's application turned the form into the (no special) symbol `quote`, just like '+ tuns into a no special symbol since it's quoted while evaluating `+` is quite different. It's not straight forward for an algol programmer as myself to find an analogy for this so it took some time getting used to :) – Sylwester Dec 18 '13 at 00:36
1

It also took me a while to understand this problem. But it's just your good-hearted lisp interpreter showing (quote (a b c)) in it's equivalent form '(a b c). Since there is no such equivalence/syntactic sugar for (quott (a b c)), it's shown as is.

AlbusMPiroglu
  • 645
  • 3
  • 13
  • I think I begin to understand. If A = `(quote (a b c))`, and B = `'(a b c)`, and A is *equal* to B and they're represented in the same way. So `(quote A)` must be equal to `(quote B)` and represented in the same way. – mosceo Dec 17 '13 at 21:48
  • @Graduate `(quote A)` is not equal to `(quote B)` as `quote` isn't evaluating parameters. You end up with symbol `A` and symbol `B` which could get evaluated to the same thing later but quote doesn't evaluate. – Loïc Faure-Lacroix Dec 18 '13 at 00:23
  • You may also want to examine standard generic method print-object. If you `defmethod print-object` for your own classes, the Lisp-printer (the same printer that prints `(quote)` as `'` ), will print your object the way you want it, everywhere. I just tried overloading print-object for symbol `'quote` and interestingly, it didn't change anything for sbcl, but worked at LispWorks. I didn't know that this was undefined behaviour by the standard and even filed a bug for sbcl which luckily got rejected soon enough :/ – AlbusMPiroglu Dec 26 '13 at 02:01