views:

200

answers:

3

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?

+4  A: 

Quoted lists (e.g. '(1 2 3)) should be treated carefully (generally as read-only). (see SO answers http://stackoverflow.com/questions/134887/when-to-use-quote-in-lisp/578365#578365 and http://stackoverflow.com/questions/134887/when-to-use-quote-in-lisp/136200#136200).

(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)
Trey Jackson
+3  A: 

In Common Lisp, quoted objects are constant literal data.

For lists:

'(1 2 3)

Above is a constant list constructed by the reader and evaluating to itself, because it is quoted.

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

(list 1 2 3)

this is a call of the function LIST with three arguments 1, 2 and 3. When evaluated the result is a 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
+10  A: 

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