Why does Scheme have both list and quote?

两盒软妹~` 提交于 2019-12-05 18:17:58

问题


Since (list 1 2 3) yields (1 2 3) and (quote (1 2 3)) yields (1 2 3), what is the rationale for having both?

Since Scheme is otherwise so spare, these must have some meaningful difference. What is that?


回答1:


In the example you mentioned quote and list have the same result because numeric constants evaluate to themselves. If you use expressions that are not self-evaluating in the list (say variables or function calls), you'll see the difference:

(quote (a b c)) will give you a list that contains the symbols a, b and c while (list a b c) will give you a list containing the values of the variables a, b and c (or an error if the variables do not exist).




回答2:


List creates a list, so (list 1 2 3) creates a three-element list.

Quote prevents evaluation. Without quote, the expression (1 2 3) would be evaluated as the function 1 called with arguments 2 and 3, which obviously makes no sense. Quote prevents evaluation and just returns the list, which is specified literally in its external printable form as (1 2 3).



来源:https://stackoverflow.com/questions/9004786/why-does-scheme-have-both-list-and-quote

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!