When to use ' (or quote) in Lisp?

2019-01-02 22:15发布

After making it through the major parts of an introductory Lisp book, I still couldn't understand what the special operator (quote) (or equivalent ') function does, yet this has been all over Lisp code that I've seen.

What does it do?

标签: lisp quote
9条回答
一纸荒年 Trace。
2楼-- · 2019-01-02 22:50

One answer to this question says that QUOTE “creates list data structures”. This isn't quite right. QUOTE is more fundamental than this. In fact, QUOTE is a trivial operator: Its purpose is to prevent anything from happening at all. In particular, it doesn't create anything.

What (QUOTE X) says is basically “don't do anything, just give me X.” X needn't be a list as in (QUOTE (A B C)) or a symbol as in (QUOTE FOO). It can be any object whatever. Indeed, the result of evaluating the list that is produced by (LIST 'QUOTE SOME-OBJECT) will always just return SOME-OBJECT, whatever it is.

Now, the reason that (QUOTE (A B C)) seems as if it created a list whose elements are A, B, and C is that such a list really is what it returns; but at the time the QUOTE form is evaluated, the list has generally already been in existence for a while (as a component of the QUOTE form!), created either by the loader or the reader prior to execution of the code.

One implication of this that tends to trip up newbies fairly often is that it's very unwise to modify a list returned by a QUOTE form. Data returned by QUOTE is, for all intents and purposes, to be considered as part of the code being executed and should therefore be treated as read-only!

查看更多
戒情不戒烟
3楼-- · 2019-01-02 22:52

Other people have answered this question admirably, and Matthias Benkard brings up an excellent warning.

DO NOT USE QUOTE TO CREATE LISTS THAT YOU WILL LATER MODIFY. The spec allows the compiler to treat quoted lists as constants. Often, a compiler will optimize constants by creating a single value for them in memory and then referencing that single value from all locations where the constant appears. In other words, it may treat the constant like an anonymous global variable.

This can cause obvious problems. If you modify a constant, it may very well modify other uses of the same constant in completely unrelated code. For example, you may compare some variable to '(1 1) in some function, and in a completely different function, start a list with '(1 1) and then add more stuff to it. Upon running these functions, you may find that the first function doesn't match things properly anymore, because it's now trying to compare the variable to '(1 1 2 3 5 8 13), which is what the second function returned. These two functions are completely unrelated, but they have an effect on each other because of the use of constants. Even crazier bad effects can happen, like a perfectly normal list iteration suddenly infinite looping.

Use quote when you need a constant list, such as for comparison. Use list when you will be modifying the result.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-02 22:52

The quote prevents execution or evaluation of a form, turning it instead into data. In general you can execute the data by then eval'ing it.

quote creates list data structures, for example, the following are equivalent:

(quote a)
'a

It can also be used to create lists (or trees):

(quote (1 2 3))
'(1 2 3)

You're probably best off getting an introductary book on lisp, such as Practical Common Lisp (which is available to read on-line).

查看更多
登录 后发表回答