Lisp quote work internally

前端 未结 2 1804
猫巷女王i
猫巷女王i 2021-01-22 11:46

How does lisp quote work internally? For example:

(quote (+ 1 (* 1 2)) )

seems to be equivalent to

(list \'+ 1 (li         


        
2条回答
  •  半阙折子戏
    2021-01-22 11:52

    quote does nothing more than return its argument unevaluated. But what is an unevaluated argument?

    When a Lisp program is defined, it is either read from textual source into s-expression form or constructed directly in terms of s-expressions. A macro would be an example of generating s-expressions. Either way there is a data structure comprising (mostly) symbols and conses that represents the program.

    Most Lisp expressions will call upon evaluation and compilation machinery to interpret this data structure as terms in a program. quote is treated specially and passed these uninterpreted symbols and conses as its argument. In short, quote does almost nothing - the value it returns already exists and is simply passed through.

    You can observe the difference between passing through and fresh construction by using eq to test the identity of the return value of quote:

    (defun f () '(1 2))
    
    (defun g () (list 1 2))
    
    (eq (f) (f)) => T
    (eq (g) (g)) => NIL
    

    As you can see, quote returns the same conses each time through.

提交回复
热议问题