In computer programming, ' is a fundamental function in most dialects of the Lisp programming language. cons'tructs memory objects which hold two values or pointers to values. These objects are referred to as cells, conses, non-atomic s-expressions, or pairs. In Lisp jargon, the expression "to cons x onto y" means to construct a new object with . The resulting pair has a left half, referred to as the CAR and CDR|, and a right half, referred to as the CAR and CDR|. It is loosely related to the object-oriented notion of a constructor, which creates a new object given arguments, and more closely related to the constructor function of an algebraic data type system. The word "cons" and expressions like "to cons onto" are also part of a more general functional programming jargon. Sometimes operators that have a similar purpose, especially in the context of list processing, are pronounced "cons".
This forms the basis of a simple, singly linked list structure whose contents can be manipulated with,, and. Note that 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:
Cons 3 onto, the empty list
Cons 2 onto the result
Cons 1 onto the result
which is equivalent to the single expression: or its shorthand: The resulting value is the list:
i.e. *--*--*--nil | | | 1 2 3 which is generally abbreviated as:
Thus, 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 will produce the list:
Another useful list procedure is Append#Lisp|, which concatenates two existing lists.
Trees
s that only store data in their leaves are also easily constructed with. For example, the code: ) results in the tree: . ) i.e. * / \ * * / \ / \ 1 2 3 4 Technically, the list in the previous example is also a binary tree, one which happens to be particularly unbalanced. To see this, simply rearrange the diagram: *--*--*--nil | | | 1 2 3 to the following equivalent: * / \ 1 * / \ 2 * / \ 3 nil
I sped up the code a bit by putting in side effects instead of having it cons ridiculously.
Functional Implementation
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 introducing 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. 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.