Search code examples
common-lisp

Is it valid to have NIL as an argument in the string comparison functions?


I am wondering whether or not (string= "abc" nil) is valid in Common Lisp. I noticed that SBCL does not complain even though nil is not a string. (string= '() nil) returns T although both arguments are not strings ...

(SBCL version: 2.2.2)


Solution

  • In Common Lisp the string comparison operators accept “string designators”. According to the Reference Manual, we have:

    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).

    So the operators accept symbols, and compare their names.

    On the other hand, the empty list is equivalent to the symbol 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.

    So the comparison is equivalent to testing the equality of the strings "NIL" and "NIL", which is obviously true.