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

前端 未结 3 1165
说谎
说谎 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: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.

提交回复
热议问题