Skip to content

LISP - Logical Predicates

In Common Lisp, logical operations are fundamental for controlling program flow in constructs like if, cond, when, and unless. Lisp’s approach to truth is both simple and powerful.

Unlike many languages that have distinct true and false boolean values, Lisp has a generalized boolean system:

  • nil: The symbol nil represents falsehood. It is the one and only false value.
  • Everything else is true: Any value that is not nil is considered true in a boolean context. This includes the symbol t, numbers (like 0 and 1), strings, lists, and all other objects.

The symbol t is the canonical or default true value, but any non-nil value works just as well.

The main logical operators in Lisp are and, or, and not.

OperatorDescription
andEvaluates its arguments from left to right. It stops and returns nil as soon as it encounters a nil argument. If all arguments are true, it returns the value of the last argument.
orEvaluates its arguments from left to right. It stops and returns the value of the first non-nil argument it finds. If all arguments are nil, it returns nil.
notTakes one argument. If the argument is nil, it returns t. If the argument is anything other than nil, it returns nil. The function null is a synonym for not.

Notice how and returns the value of the last form if all are true.

(write (and t t)) ; All true, returns last value -> T
(terpri)
(write (and t "hello" 1)) ; All true, returns last value -> 1
(terpri)
(write (and t nil 1)) ; Encounters nil, short-circuits -> NIL
T
1
NIL

Notice how or returns the first true value it finds.

(write (or nil nil)) ; All false -> NIL
(terpri)
(write (or nil t 1)) ; Finds t, short-circuits -> T
(terpri)
(write (or nil "hello" 1)) ; Finds "hello", short-circuits -> "hello"
NIL
"hello"
T

not simply inverts any generalized boolean.

(write (not t)) ; True value becomes nil -> NIL
(terpri)
(write (not 123)) ; True value becomes nil -> NIL
(terpri)
(write (not nil)) ; The only false value becomes t -> T
(terpri)
(write (null nil)) ; `null` is a synonym for `not` -> T
NIL
NIL
T
T

Practical Application: Conditional Execution

Section titled “Practical Application: Conditional Execution”

The short-circuiting behavior of and and or is often used idiomatically for concise conditional logic.

(defun describe-item (item)
(and item (format t "Item is: ~a~%" item)))
(describe-item "A book") ; -> Prints "Item is: A book"
(describe-item nil) ; -> Does nothing and returns NIL