Lisp: Is there a difference between 'nil and nil?
(quote <anything>)
means literally <anything>
. The designation '<anything>
means (quote <anything>)
, and no matter what <anything>
, it just returns without evaluation.
It also nil
evaluates itself.
Other objects quoted as literals also evaluate themselves: certain characters and all non-character atoms are.
What is the difference between '2
and 2
? They both rate up 2
!
Also, what is the difference between '"abc"
and "abc"
and between :foo
and ':foo
?
The difference is what '2
is form (quote 2)
versus what 2
is simple 2
. They rate the same thing, but not the same thing.
For Lisp evaluation, means that the base point is treated as the source code of the expression. Two expressions can have the same meaning, but be made from different data. For example, 4
, (+ 2 2)
and (* 2 2)
.
Tell me what's the difference between 4
and (+ 2 2)
?
If 4
and (+ 2 2)
both produce 4, then why '4
create 4
, but '(+ 2 2)
create (+ 2 2)
?
The quote means "give me this piece of code as a binding, not the value it stands for."
source to share
When you evaluate NIL
, you get the value of a variable named NIL , and when you evaluate 'NIL
, you get a character named NIL. However, these two things are defined by the specification as the same object. See Hyperspec for Zero :
nil n. an object that is both a symbol with a name
"NIL"
in a packageCOMMON-LISP
, an empty list, a boolean (or generic boolean) representing false, and an empty type name.
You can check it yourself:
(eq NIL 'NIL) ==> T
However, the equivalence ceases. ''NIL
matches the list (quote NIL)
.
source to share
It makes no difference if you only consider the result of the evaluation, i.e.
nil
and
'nil
evaluate the same value (namely, nil
). However, there is a true difference in the reader's attitude as
nil ===> nil
then
'nil ===> (quote nil)
This is interesting especially if you have nested forms like
((nil) 'nil)
which reads like
((nil) (quote nil))
source to share