|
For other uses, see Cons (disambiguation).
In computer programming, cons (pronounced /ˈkɒnz/ or /ˈkɒns/) is a fundamental function in all dialects of the Lisp programming language. 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". (A good example is the :: operator in ML and the : operator in Haskell, which adds an element to the beginning of a list.)
UseAlthough cons cells can be used to implement ordered pairs of simplex data, they are more commonly used to construct more complex compound data structures, notably lists and binary trees. For example, the Lisp expression (1 . 2) ListsIn 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
which is equivalent to the single expression: (cons 1 (cons 2 (cons 3 nil))) or its shorthand: (list 1 2 3) The resulting value is the list: (1 . (2 . (3 . nil))) i.e. *--*--*--nil | | | 1 2 3 which is generally abbreviated as: (1 2 3) Thus, (5 1 2 3) Another useful list procedure is TreesBinary trees that only store data in their leaves, are also easily constructed with (cons (cons 1 2) (cons 3 4)) results in the tree: ((1 . 2) . (3 . 4)) i.e. * / \ * * / \ / \ 1 2 3 4 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: *--*--*--nil | | | 1 2 3 to the following equivalent:
*
/ \
1 *
/ \
2 *
/ \
3 nil
Use in conversationCons 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:
Not fundamentalSince Lisp has first-class functions, all data structures, including cons cells, are not fundamentally necessary to the language, since all data structures can be implemented using functions. For example, in Scheme: (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) The above code re-implements the cons, car, and cdr operations, using a function as the "cons cell". This is the 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. TriviaUnder the name of the "cons box" (which the cell is called in older Lisp texts like the John Allen classic "Anatomy of Lisp"), the cons has been the symbol and logotype of the computer science undergraduate education program at Uppsala University since its inception in 1981[1]. See also |
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.