In Lisp (Clojure, Emacs Lisp), what is the difference between list and quote?

前端 未结 3 1057
说谎
说谎 2020-12-29 20: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 pro

相关标签:
3条回答
  • 2020-12-29 21:12

    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]
    
    0 讨论(0)
  • 2020-12-29 21:31

    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.

    0 讨论(0)
  • 2020-12-29 21:32

    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)
    
    0 讨论(0)
提交回复
热议问题