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?
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'list
s variable number of arguments.The primary difference is that
quote
prevents evaluation of the elements, whereaslist
does not:For this reason (among others), it is idiomatic clojure to use a vector when describing a literal collection:
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:
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.this is a call of the Common Lisp function
LIST
with three arguments1
,2
and3
. When evaluated the result is a fresh new list(1 2 3)
.Similar:
One is the literal data and the other is a function call that constructs such a data structure.