Soyuz
Soyuz

Reputation: 1077

Lisp: Is there any difference between 'nil and nil?

Both

(not 'nil)

and

(not nil)

evaluate to T, so is there any difference between 'nil and nil? And what about ''nil? If ''nil evaluates to 'nil, shouldn't ''nil evaluate to nil as well?

Upvotes: 3

Views: 376

Answers (3)

Kaz
Kaz

Reputation: 58627

(quote <anything>) evaluates to <anything>, literally. The notation '<anything> means (quote <anything>), and regardless of what <anything> is, it is simply returned without being evaluated.

Furthermore, nil evaluates to itself.

Other objects, when quoted as literals, also evaluate to themselves: certain symbols and all non-symbolic atoms are this way.

What is the difference between '2 and 2? They both evaluate to 2!

Also, what is the difference between '"abc" and "abc", and between :foo and ':foo?

The difference is that '2 is the form (quote 2) whereas 2 is just 2. They evaluate to the same thing but aren't the same thing.

To evaluate Lisp means that a datum is treated as the source code of an expression. Two expressions can have the same value, yet be made of different data. For instance 4, (+ 2 2) and (* 2 2).

Say, what's the difference between 4 and (+ 2 2)?

If 4 and (+ 2 2) both produce 4, why does '4 produce 4, but '(+ 2 2) produces (+ 2 2)?

Quote means, "give me this piece of program code as a datum, rather than the value which it denotes."

Upvotes: 8

Gareth Rees
Gareth Rees

Reputation: 65854

When you evaluate NIL you get the value of the variable named NIL, and when you evaluate 'NIL you get the symbol named NIL. However, these two things are defined by the spec to be the same object. See the Hyperspec on 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.

You can check that for yourself:

(eq NIL 'NIL) ==> T

However, the equivalence stops there. ''NIL evaluates to the list (quote NIL).

Upvotes: 6

Dirk
Dirk

Reputation: 31061

There is no difference, as long as you consider only the result of evaluation, i.e.,

nil

and

'nil

evaluate to the same value (namely, nil). There is, however, a true difference, as far as the reader is concerned, since

nil  ===> nil

whereas

'nil ===> (quote nil)

This is interesting in particular, if you have nested forms like

((nil) 'nil)

which is read as

((nil) (quote nil))

Upvotes: 1

Related Questions