Skip to main content

consp

consp Function

Syntax:

consp object → generalized-boolean

Arguments and Values:

object—an object.

generalized-boolean—a generalized boolean.

Description:

Returns true if object is of type cons; otherwise, returns false.

Examples:

(consp nil) → false 
(consp (cons 1 2)) → true
The *empty list* is not a *cons*, so
(consp()) *≡* (consp ’nil) → false

See Also:

listp

Notes:

(consp object) (typep object ’cons) (not (typep object ’atom)) (typep object ’(not atom))

Expanded Reference: consp

Testing if an object is a cons cell

consp returns true if its argument is a cons cell, and false otherwise.

(consp '(1 2 3))
=> T

(consp '(a . b))
=> T

(consp nil)
=> NIL

Distinguishing consp from listp

consp returns false for NIL, while listp returns true. This is the key difference: NIL is a list but not a cons.

(consp nil)
=> NIL

(listp nil)
=> T

(consp '(1))
=> T

(listp '(1))
=> T

Using consp for type checking in recursive functions

consp is commonly used to check whether you have a cons to recurse into or an atom to process.

(defun count-conses (tree)
"Count the number of cons cells in a tree."
(if (consp tree)
(+ 1
(count-conses (car tree))
(count-conses (cdr tree)))
0))

(count-conses '(a (b c) d))
=> 5

Non-list objects are not conses

Numbers, strings, symbols, and other atoms all return NIL for consp.

(consp 42)
=> NIL

(consp "hello")
=> NIL

(consp 'foo)
=> NIL

(consp #\A)
=> NIL