gpt4 book ai didi

lisp - Common Lisp 中的 '() 与 ()

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:15 25 4
gpt4 key购买 nike

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

(append '() '())

...我相信可以这样写:

(append () ())

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

这个历史包袱是否来自其他/较旧的相关语言,这些语言已经被传统所继承?

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

最佳答案

引用具有非常丰富的属性,清楚地表明您的意思是 token 应该是文字数据。当您或同事多年后重新访问代码时更是如此。

您必须使用()(不带引号)以非评估形式声明一个空列表,例如参数列表或类父类(super class)和插槽,其中引号实际上会导致伤害。实际上,您也可以使用 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.

关于lisp - Common Lisp 中的 '() 与 (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37424777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com