在字符串比较函数中将 NIL 作为参数是否有效?
Is it valid to have NIL as an argument in the string comparison functions?
我想知道 (string= "abc" nil)
在 Common Lisp 中是否有效。我注意到即使 nil
不是字符串,SBCL 也不会抱怨。 (string= '() nil)
returns T
虽然两个参数都不是字符串 ...
(SBCL 版本:2.2.2)
在 Common Lisp 中,字符串比较运算符接受“字符串指示符”。根据Reference Manual,我们有:
string designator n. a designator for a string; that is, an object that denotes a string and that is one of: a character (denoting a singleton string that has the character as its only element), a symbol (denoting the string that is its name), or a string (denoting itself).
因此运算符接受符号,并比较它们的名称。
另一方面,空列表等价于符号NIL
:
nil n. the object that is at once the symbol named "NIL" in the COMMON-LISP package, the empty list, the boolean (or generalized boolean) representing false, and the name of the empty type.
所以比较相当于测试字符串"NIL"
和"NIL"
是否相等,这显然是正确的。
我想知道 (string= "abc" nil)
在 Common Lisp 中是否有效。我注意到即使 nil
不是字符串,SBCL 也不会抱怨。 (string= '() nil)
returns T
虽然两个参数都不是字符串 ...
(SBCL 版本:2.2.2)
在 Common Lisp 中,字符串比较运算符接受“字符串指示符”。根据Reference Manual,我们有:
string designator n. a designator for a string; that is, an object that denotes a string and that is one of: a character (denoting a singleton string that has the character as its only element), a symbol (denoting the string that is its name), or a string (denoting itself).
因此运算符接受符号,并比较它们的名称。
另一方面,空列表等价于符号NIL
:
nil n. the object that is at once the symbol named "NIL" in the COMMON-LISP package, the empty list, the boolean (or generalized boolean) representing false, and the name of the empty type.
所以比较相当于测试字符串"NIL"
和"NIL"
是否相等,这显然是正确的。