5 ms·
Can you type declare the fact that NIL is both a list and an atom in Swift?
by kruhft 10y ago
Can you type declare the fact that NIL is both a list and an atom in Swift?
- akkartik 10y agoIt doesn't have to be. In Common Lisp nil is a symbol but not a list. In Racket '() is a list but not a symbol (or a cons). FWIW, in my rite-of-passage lisp-in-C implementation the type of nil and () is nil, just like the car or cdr of nil: http://akkartik.name/post/wart http://akkartik.name/post/wart
- reikonomusha 10y agoIn Common Lisp, NIL is a symbol and a list and a Boolean.
- akkartik 10y agoAh, you're right. It's a list but not a cons. $ sbcl * (symbolp nil) T * (listp nil) T * (consp nil) NIL Is there a boolean predicate? My Common Lisp is rusty and I don't see one at https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node73.html#SECTION001022000000000000000 https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node73.html#S....
- wtbob 10y ago> Is there a boolean predicate? Nope, because everything is a Boolean: NIL is false, and everything else is true.
- reikonomusha 10y agoThis is not quite precisely true. Everything is a generalized boolean, but not a boolean. Only T and NIL are of type BOOLEAN, which you can check with the function: (defun booleanp (x) (TYPEP x 'BOOLEAN))
- wtbob 10y agoYou're right, of course. Still, 'everything is a boolean' is close enough for government work:-)
- kazinator 10y agoAlso, the type of NIL in Common Lisp is NULL. There is a NIL type, but it is empty: there exists no value which is an instance of the NIL type. NIL is a subtype of every type (including itself), and the type T is a supertype of every type, including itself. This situation with NIL and T on opposite ends of the "type spindle" is quite clever and subtly better than making NIL a type which contains NIL.
- akkartik 10y agoThanks for the pointer! I'd never seen http://www.lispworks.com/documentation/lw70/CLHS/Body/t_null.htm#null http://www.lispworks.com/documentation/lw70/CLHS/Body/t_null... before, in spite of poring over the CLHS for many hours over almost a decade. Can you suggest any example Common Lisp programs that print the _types_ NULL or NIL or T? Or show off the benefits of this spindle arrangement? I can intuitively see the benefits of this approach, but an example makes things much more memorable.
- kazinator 10y agoThe nice thing about T is that it's the first letter of True and of Type. E.g. ;; catch-all method for any type (defmethod foo ((arg t)) ...) ;; (defmethod foo (arg ...) ...) ;; alternative spelling: omit class NIL is the empty list and that is mnemonic for "empty type". For instance, the intersection of two mutually exclusive types is the empty type. Nothing can be both a cons and an atom, so the type denoted by the type expression (and cons atom) is empty; and that is nil. The set manipulation which underlies types needs an empty set, and nil fill the need for that empty set (pun intended). NULL is useful in writing methods that capture NIL (the only instance of NULL). That may be recognized as the "null object pattern": (defmethod foo ((arg null)) ;; (foo nil) called ) (defmethod foo ((arg number)) ;; (foo <number>) called ) (defmethod foo ((arg t)) ;; (foo <anything else>) called )
- sdegutis 10y agoSure, just make two protocols that nil's class conforms to, one for list and one for atom.
- kruhft 10y agoCool. That's one of the biggest pains I've found with implementing a lisp in a typed language.