Skip to main content

cons

cons System Class

Class Precedence List:

cons, list, sequence, t

Description:

A cons is a compound object having two components, called the car and cdr . These form a dotted pair . Each component can be any object.

Compound Type Specifier Kind:

Specializing.

Compound Type Specifier Syntax:

(cons [car-typespec [cdr-typespec]])

Compound Type Specifier Arguments:

car-typespec—a type specifier , or the symbol *. The default is the symbol *.

cdr-typespec—a type specifier , or the symbol *. The default is the symbol *.

Compound Type Specifier Description:

This denotes the set of conses whose car is constrained to be of type car-typespec and whose cdr is constrained to be of type cdr-typespec. (If either car-typespec or cdr-typespec is *, it is as if the type t had been denoted.)

See Also:

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

Expanded Reference: cons (System Class)

Basic cons cell

A cons is a compound object with two components: a car and a cdr. Every non-empty list is built from cons cells.

(typep '(a . b) 'cons)
=> T

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

(typep nil 'cons)
=> NIL

Class precedence

The class precedence list for cons is: cons, list, sequence, t.

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

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

Compound type specifier

The compound type specifier (cons car-type cdr-type) constrains the types of the car and cdr components.

(typep '(1 . "hello") '(cons integer string))
=> T

(typep '(1 . 2) '(cons integer string))
=> NIL

(typep '(a . b) '(cons symbol symbol))
=> T

Cons vs null partition the list type

Every list is either a cons (non-empty) or null (the empty list). There is no overlap.

(typep '(a) 'cons)
=> T

(typep '() 'cons)
=> NIL

(typep '() 'null)
=> T

Cons cells are the building blocks of lists

A proper list is a chain of cons cells where the cdr of the last cons is NIL.

;; (1 2 3) is really (cons 1 (cons 2 (cons 3 nil)))
(equal '(1 2 3)
(cons 1 (cons 2 (cons 3 nil))))
=> T