Common Lisp 中的 '() 与 ()

'() vs () in Common Lisp

在 Common Lisp 中,() 似乎是一种自我评估的形式。也就是说,它计算自身(或其别名 nil)。所以似乎没有必要引用它。但是在我的 quicklisp 目录上使用 grep 会发现许多 '() 的实例,这些实例是由许多不同的人在许多不同的项目中编写的。写出引用的版本是否有技术原因? Common Lisp the Lanugage, 2nd Edition, Section 1.2.2,提到了你可能想用 () 强调空列表和 nil 强调布尔值 false 的风格差异,但没有涵盖这个问题。 Steele 使用的示例之一是:

(append '() '())

...我认为可以写成:

(append () ())

...那么为什么要把额外的 QUOTEs 放在那里?当然,它不会伤害任何东西。在文体上,一种形式是否普遍优于另一种形式?有人肯定会提出这样的情况,引用形式使添加元素变得更简单,以防您改变主意,并且真的想要一个非空的文字列表。或者在使用引号时存在一定的对称性,因为非空文字列表也需要它。

这个历史包袱是否挂在other/older相关语言的传统上?

这与 Scheme 不同,在 Scheme 中您需要引用它。而且似乎您不必在 elisp 中引用它,所以也许以一种迂回的方式它可能与 lisp-1 和 lisp-2 相关?

在 Common Lisp 中,()nil 都是 nil 的表示,并且是自求值的。引用的所有内容都会对其参数进行评估,但由于参数是 nil 这是自我评估的结果是相同的。编译后的代码很可能是相同的。

Scheme 最初是在 CL 的前身下解释的,在第一份报告中,他们或多或少只是制作了一个带有词法闭包和一个名称空间的 lisp,仅此而已。它看起来更像 CL 而不是 Scheme,但它是一个 LISP1,因此空列表规则不能是关于 Lisp1 与 Lisp2 的,因为 Scheme 是第一份报告中的 Lisp1,不需要引用并且 nil 作为假值。

对于报告的每个连续版本,Scheme 都在改进其语法和命名转换。修订做了细微的改变,他们不怕做出重大改变。他们引入了新的布尔值,更改了名称,将空列表的布尔值从 false 更改为 true,删除了 nilt 作为 #t#f 的别名,并被视为() 无效表达式,因此需要 '()

既然你在 CL 中如何做并不重要,也许一些使用两种语言编写代码的人只是想在他们的生活中有一些对称性。我当然会这样做,但我不能代表所有的阴谋家。

有理由更喜欢将 '() 写成 (),即使 () 是自我评估的。它使代码更易于阅读,因为在视觉上很明显您正在处理数据,而不是计算的形式。

这与更喜欢写'#(foo bar baz (+ 1 2))而不是#(foo bar baz (+ 1 2))的原因类似。两者的计算结果相同,但前者在视觉上明显表明 foobarbaz(+ 1 2) 未被计算。

我从不引用 NIL()T,当然也不是数字或字符串。

如果我阅读 '() 某人的代码,我可能会对作者的意图感到困惑,并且可能 his/her 理解 Common Lisp 中的引用(这里的初学者问题往往表现出对引号)。

我不能说某些 CL 的实现在某些时候需要引用 () 的可能性,但更有可能的是作者有使用其他风格的 Lisp 和出于习惯或故意,试图将这种经验应用于 Common Lisp。

我认为引用 () 唯一有意义的情况是当先前非空的常量列表被清空并且有人懒得删除引用时。

(defvar *options* '(debug))

...变成

(defvar *options* '())

如果您打算稍后更改默认值,保留报价甚至是有意义的。

该引文提供了非常丰富的信息 属性,清楚地表明您的意思是令牌应该是文字数据。当您或同事多年后重新访问代码时更是如此。

您必须使用()(不带引号)以非评估形式声明空列表,例如参数列表或class super-classes and slots ,引用实际上会造成伤害。实际上,您也可以使用 nil,但为了清楚起见,当声明的内容是列表时,您不应该使用。


这是来自 the specification 的相关摘录:

1.4.1.4.4 NIL

nil has a variety of meanings. It is a symbol in the COMMON-LISP package with the name "NIL", it is boolean (and generalized boolean) false, it is the empty list, and it is the name of the empty type (a subtype of all types).

Within Common Lisp, nil can be notated interchangeably as either NIL or (). By convention, the choice of notation offers a hint as to which of its many roles it is playing.

For Evaluation?  Notation  Typically Implied Role       
----------
Yes              nil       use as a boolean.            
Yes              'nil      use as a symbol.             
Yes              '()       use as an empty list         
No               nil       use as a symbol or boolean.  
No               ()        use as an empty list.        

Figure 1-1. Notations for NIL

Within this document only, nil is also sometimes notated as false to emphasize its role as a boolean.

For example:

(print ())                          ;avoided
(defun three nil 3)                 ;avoided 
'(nil nil)                          ;list of two symbols
'(() ())                            ;list of empty lists
(defun three () 3)                  ;Emphasize empty parameter list.
(append '() '()) =>  ()              ;Emphasize use of empty lists
(not nil) =>  true                   ;Emphasize use as Boolean false
(get 'nil 'color)                   ;Emphasize use as a symbol

A function is sometimes said to “be false” or “be true” in some circumstance. Since no function object can be the same as nil and all function objects represent true when viewed as booleans, it would be meaningless to say that the function was literally false and uninteresting to say that it was literally true. Instead, these phrases are just traditional alternative ways of saying that the function “returns false” or “returns true,” respectively.