Skip to main content

list

list System Class

Class Precedence List:

list, sequence, t

Description:

A list is a chain of conses in which the car of each cons is an element of the list, and the cdr of each cons is either the next link in the chain or a terminating atom.

A proper list is a chain of conses terminated by the empty list, (), which is itself a proper list. A dotted list is a list which has a terminating atom that is not the empty list. A circular list is a chain of conses that has no termination because some cons in the chain is the cdr of a later cons.

Dotted lists and circular lists are also lists, but usually the unqualified term “list” within this specification means proper list. Nevertheless, the type list unambiguously includes dotted lists and circular lists.

For each element of a list there is a cons. The empty list has no elements and is not a cons. The types cons and null form an exhaustive partition of the type list.

See Also:

Section 2.4.1 (Left-Parenthesis), Section 22.1.3.5 (Printing Lists and Conses)

Expanded Reference: list (System Class)

Type checking with list

The type list is the union of cons and null. It includes all proper lists, dotted lists, and circular lists.

(typep '(1 2 3) 'list)
=> T

(typep nil 'list)
=> T

(typep 42 'list)
=> NIL

Three kinds of lists

Lists come in three varieties: proper lists (terminated by NIL), dotted lists (terminated by a non-NIL atom), and circular lists.

;; Proper list
(typep '(a b c) 'list)
=> T

;; Dotted list
(typep '(a b . c) 'list)
=> T

;; NIL (the empty list)
(typep '() 'list)
=> T

Cons and null partition list

Every object of type list is either of type cons (non-empty list) or of type null (empty list). There is no third possibility.

(let ((items (list '(a b) nil '(x) '() '(1 . 2))))
(every (lambda (x) (or (typep x 'cons) (typep x 'null)))
items))
=> T

Class precedence list

The class precedence list for list is: list, sequence, t. Since list is a subtype of sequence, all sequence functions work on lists.

(subtypep 'list 'sequence)
=> T
=> T

(length '(a b c))
=> 3

(elt '(a b c) 1)
=> B

Difference between list type and listp

The type list and the predicate listp test the same thing: whether an object is a cons or NIL.

(values (typep '(1) 'list) (listp '(1)))
=> T
=> T

(values (typep 42 'list) (listp 42))
=> NIL
=> NIL