Skip to main content

null

null Function

Syntax:

null object → boolean

Arguments and Values:

object—an object.

boolean—a boolean.

Description:

Returns t if object is the empty list; otherwise, returns nil.

Examples:

(null()) → T 
(null nil) → T
(null t) → NIL
(null 1) → NIL

See Also:

not

Notes:

null is intended to be used to test for the empty list whereas not is intended to be used to invert a boolean (or generalized boolean). Operationally, null and not compute the same result; which to use is a matter of style.

(null object) (typep object ’null) (eq object ’())

Expanded Reference: null

Testing for the empty list

null returns T if its argument is NIL (the empty list), and NIL otherwise.

(null nil)
=> T

(null '())
=> T

(null '(1 2 3))
=> NIL

Non-NIL values return NIL

Any object that is not NIL causes null to return NIL.

(null t)
=> NIL

(null 0)
=> NIL

(null "")
=> NIL

null vs. not

null and not compute the same result. The convention is to use null when testing for the empty list and not when inverting a boolean.

;; Use null when checking list emptiness
(null (cdr '(only-one)))
=> T

;; Use not when inverting a boolean condition
(not (> 3 5))
=> T

Common pattern: checking if a list is empty

null is the standard idiom for testing end-of-list in recursive functions.

(defun my-length (lst)
(if (null lst)
0
(+ 1 (my-length (cdr lst)))))

(my-length '(a b c d))
=> 4

(my-length nil)
=> 0

Using null with conditional expressions

null integrates naturally with when, unless, and cond.

(let ((items '()))
(when (null items)
"The list is empty"))
=> "The list is empty"