LISP - Logical Predicates
Lisp - Logical Operators and Predicates
Section titled “Lisp - Logical Operators and 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.
The Concept of Generalized Booleans
Section titled “The Concept of Generalized Booleans”Unlike many languages that have distinct true and false boolean values, Lisp has a generalized boolean system:
nil: The symbolnilrepresents falsehood. It is the one and only false value.- Everything else is true: Any value that is not
nilis considered true in a boolean context. This includes the symbolt, numbers (like0and1), 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.
Core Logical Operators
Section titled “Core Logical Operators”The main logical operators in Lisp are and, or, and not.
| Operator | Description |
|---|---|
and | Evaluates 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. |
or | Evaluates 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. |
not | Takes 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. |
Example: The and Operator
Section titled “Example: The and Operator”Notice how and returns the value of the last form if all are true.
main.lisp
Section titled “main.lisp”(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 -> NILOutput
Section titled “Output”T1NILExample: The or Operator
Section titled “Example: The or Operator”Notice how or returns the first true value it finds.
main.lisp
Section titled “main.lisp”(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"Output
Section titled “Output”NIL"hello"TExample: The not Operator
Section titled “Example: The not Operator”not simply inverts any generalized boolean.
main.lisp
Section titled “main.lisp”(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` -> TOutput
Section titled “Output”NILNILTTPractical 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