Yeah, t and nil are self-evaluating symbols in Lisps that use them, like Common Lisp and Emacs Lisp. Don't sweat it, just keep that in mind.
Emacs
A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!
Get Emacs
Rules
- Posts should be emacs related
- Be kind please
- Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.
Emacs Resources
Emacs Tutorials
- Beginner’s Guide to Emacs
- Absolute Beginner's Guide to Emacs
- How to Learn Emacs: A Hand-drawn One-pager for Beginners
Useful Emacs configuration files and distributions
Quick pain-saver tip
There are a variety of "self-quoting" constructs in elisp (including all :keywords
!).
(eq (quote nil) nil) ; ==> t
(eq (quote t) t) ; ==> t
(eq (quote :some-keyword) :some-keyword) ; ==> t
I feel like that image is distracting from the point.
Nil is a value and also a symbol. (In some Lisps. Definitely in Common Lisp.)
Nil may be written as a symbol nil or it may be written as an empty list (). Same thing either way. Nil evaluates to itself. In contrast, most symbols (but certainly not all) evaluate to their bound value. [I'm aware that statement is a simplification, gotta start somewhere.]
In any evaluation context, a symbol such as foo is evaluated. Quoting that symbol means evaluation "consumes" the quote and leaves just the symbol. So the evaluation of 'j is j, and so forth. Naturally the evaluation of 'nil is nil. But without evaluation, 'j differs from j and 'nil differs from nil.
I was actually suprised to find that the empty list is a symbol, the same one referred to by nil
. I thought they would refer to some special singular 'nil' value with its own type.
(symbolp '()) ; ==> t
(eq nil '()) ; ==> t
(eq 'nil '()) ; ==> t
(eq 'nil nil) ; ==> t
(symbol-name '()) ; ==> "nil"
nil
being self-evaluating is not strange. Since nil
is a symbol it can be defined as a constant. So there is nothing to stop a Lisp implementation saying (in CL):
(defconstant nil 'nil)
just the same as
(defconstant t 't)
What is strange is these things:
strange thing | true in CL? | true in elisp? | true in Scheme? |
---|---|---|---|
() is a list but not a cons |
yes | yes | yes |
() is both a list and a symbol |
yes | yes | no |
() is self-evaluating |
yes | yes | no |