Although cons cells can be used to hold ordered pairs of data, they are more commonly used to construct more complex compound data structures, notably lists and binary trees.
For example, the Lisp expression (cons 1 2) constructs a cell holding 1 in its left half (the so-called car field) and 2 in its right half (the cdr field). In Lisp notation, the value (cons 1 2) looks like:
Note the dot between 1 and 2; this indicates that the S-expression is a "dotted pair" (a so-called "cons pair"), rather than a "list."
In Lisp, lists are implemented on top of cons pairs. More specifically, any list structure in Lisp is either:
This forms the basis of a simple, singly linked list structure whose contents can be manipulated with cons, car, and cdr. Note that nil is the only list that is not also a cons pair. As an example, consider a list whose elements are 1, 2, and 3. Such a list can be created in three steps:
which is equivalent to the single expression:
or its shorthand:
The resulting value is the list:
i.e.
which is generally abbreviated as:
Thus, cons can be used to add one element to the front of an existing linked list. For example, if x is the list we defined above, then (cons 5 x) will produce the list:
Another useful list procedure is append, which concatenates two existing lists (i.e. combines two lists into a single list).
Binary trees that only store data in their leaves are also easily constructed with cons. For example, the code:
results in the tree:
Technically, the list (1 2 3) in the previous example is also a binary tree, one which happens to be particularly unbalanced. To see this, simply rearrange the diagram:
to the following equivalent:
Cons can refer to the general process of memory allocation, as opposed to using destructive operations of the kind that would be used in an imperative programming language. For example:
I sped up the code a bit by putting in side effects instead of having it cons ridiculously.
Since Lisp has first-class functions, all data structures, including cons cells, can be implemented using functions. For example, in Scheme:
This technique is known as Church encoding. It re-implements the cons, car, and cdr operations, using a function as the "cons cell". Church encoding is a usual way of defining data structures in pure lambda calculus, an abstract, theoretical model of computation that is closely related to Scheme.
This implementation, while academically interesting, is impractical because it renders cons cells indistinguishable from any other Scheme procedure, as well as introduces unnecessary computational inefficiencies.
However, the same kind of encoding can be used for more complex algebraic data types with variants, where it may even turn out to be more efficient than other kinds of encoding.1 This encoding also has the advantage of being implementable in a statically typed language that doesn't have variants, such as Java, using interfaces instead of lambdas.
"Efficient Interpretation by Transforming Data Types and Patterns to Functions" (PDF). Archived from the original (PDF) on 2010-03-31. Retrieved 2009-03-01. https://web.archive.org/web/20100331083602/http://www.st.cs.ru.nl/papers/2006/janj2006-TFP06-EfficientInterpretation.pdf ↩