Go to the first, previous, next, last section, table of contents.


Truth and Falsehood in Lisp

There is an important aspect to the truth test in an if expression. So far, we have spoken of `true' and `false' as values of predicates as if they were new kinds of Lisp objects. In fact, `false' is just our old friend nil. Anything else--anything at all--is `true'.

The expression that tests for truth is interpreted as true if the result of evaluating it is a value that is not nil. In other words, the result of the test is considered true if the value returned is a number such as 47, a string such as "hello", or a symbol (other than nil) such as flowers, or a list, or even a buffer!

Before illustrating this, we need an explanation of nil.

In Lisp, the symbol nil has two meanings. First, it means the empty list. Second, it means false and is the value returned when a true-or-false-test tests false. nil can be written as an empty list, (), or as nil. As far as the Lisp interpreter is concerned, () and nil are the same. Humans, however, tend to use nil for false and () for the empty list.

In Lisp, any value that is not nil---is not the empty list--is considered true. This means that if an evaluation returns something that is not an empty list, an if expression will test true. For example, if a number is put in the slot for the test, it will be evaluated and will return itself, since that is what numbers do when evaluated. In this case, the if expression will test true. The expression tests false only when nil, an empty list, is returned by evaluating the expression.

You can see this by evaluating the two expressions in the following examples.

In the first example, the number 4 is evaluated as the test in the if expression and returns itself; consequently, the then-part of the expression is evaluated and returned: `true' appears in the echo area. In the second example, the nil indicates false; consequently, the else-part of the expression is evaluated and returned: `false' appears in the echo area.

(if 4
    'true
  'false)

(if nil
    'true
  'false)

Incidentally, if some other useful value is not available for a test that returns true, then the Lisp interpreter will return the symbol t for true. For example, the expression (> 5 4) returns t when evaluated, as you can see by evaluating it in the usual way:

(> 5 4)

On the other hand, this function returns nil if the test is false.

(> 4 5)


Go to the first, previous, next, last section, table of contents.