32

From reading introductory material on Lisp, I now consider the following to be identical:

(list 1 2 3)

'(1 2 3)

However, judging from problems I face when using the quoted form in both Clojure and Emacs Lisp, they are not the same. Can you tell me what the difference is?

Rainer Joswig
  • 131,638
  • 10
  • 209
  • 336
neo
  • 323
  • 3
  • 4
  • They are quit similar in fact take a look at my question [here](http://stackoverflow.com/questions/32927115/lisp-quote-work-internally?noredirect=1#comment53680808_32927115) – Lime Oct 03 '15 at 22:14

4 Answers4

37

The primary difference is that quote prevents evaluation of the elements, whereas list does not:

user=> '(1 2 (+ 1 2))
(1 2 (+ 1 2))
user=> (list 1 2 (+ 1 2))
(1 2 3)

For this reason (among others), it is idiomatic clojure to use a vector when describing a literal collection:

user=> [1 2 (+ 1 2)]
[1 2 3]
Alex Taggart
  • 7,695
  • 25
  • 30
  • 1
    this answer seems dishonest as it does not mention that `(list 1 2 '(+ 1 2))` is still a list but the third element is not evaluated but it does not express the difference between `'(1 2 (+ 1 2))` and `(list '1 '2 '(+ 1 2))` – Dmitry Nov 16 '16 at 17:39
11

Quoted lists (e.g. '(1 2 3)) should be treated carefully (generally as read-only). (see SO answers When to use 'quote in Lisp and When to use 'quote in Lisp).

(list 1 2 3) will "cons" up a fresh list, independent of all others.

You can see an example of a pitfall of using quoted lists in the manual for nconc.

And, as you probably know, when you call 'list - the arguments will obviously be evaluated versus the contents of a quoted list. And 'quote takes a single argument, versus 'lists variable number of arguments.

(list (+ 1 2) 3)     -->  (3 3)
(quote ((+ 1 2) 3))  -->  ((+ 1 2) 3)
Community
  • 1
  • 1
Trey Jackson
  • 72,100
  • 11
  • 192
  • 225
11

In Common Lisp, quoted objects are constant literal data. You should not modify this data, as the consequences are undefined. Possible consequences are: modification of shared data, attempt to modify read-only data, an error might be signalled, it might just work, ...

For lists:

'(1 2 3)

Above is a constant list, which will be constructed by the reader and evaluating to itself, because it is quoted. If it appears in Lisp code, a compiler will embed this data somehow in the FASL code.

(quote (1 2 3)) is another way to write it.

(list 1 2 3)

this is a call of the Common Lisp function LIST with three arguments 1, 2 and 3. When evaluated the result is a fresh new list (1 2 3).

Similar:

'(1 . 2)   and  (cons 1 2)

'#(1 2 3)  and  (vector 1 2 3)

One is the literal data and the other is a function call that constructs such a data structure.

Rainer Joswig
  • 131,638
  • 10
  • 209
  • 336
0

Their relation can be analogous to function invocation with 'function name' and funcall.

When you have no idea what function you'll get at runtime, you use funcall.

When you have no idea what element you may get at runtime, your use list.


For people like me who get confused because of existence of backquote and count it as quote tacitly.

backquote is not quote

It's a reader-macro that expands into quote, list or others:

(macroexpand ''(1 2));=> '(1 2)
(macroexpand '`(1 2));=> '(1 2)
(macroexpand '`(1 ,2));=> (list 1 2)
(macroexpand '`(1 ,@foo));=> (cons 1 foo)
(macroexpand '`(1 ,@foo 2));=> (cons 1 (append foo '(2)))
nichijou
  • 444
  • 3
  • 11