Cons

Cons

In computer programming, cons (pronEng|ˈkɒnz or IPA|/ˈkɒns/) is a fundamental function in all dialects of the Lisp programming language. cons "constructs" (hence the name) memory objects which hold two values or pointers to values. These objects are referred to as (cons) cells, conses, or (cons) pairs. In Lisp jargon, the expression "to cons "x" onto "y" means to construct a new object with (cons "x" "y"). The resulting pair has a left half, referred to as the car (the first element), and a right half (the second element), referred to as the 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". (A good example is the :: operator in ML and the : operator in Haskell, which adds an element to the beginning of a list.)

Use

Although 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 (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:

(1 . 2)

Lists

In Lisp, lists are implemented on top of cons pairs. More specifically, any list structure in Lisp is either:

#An empty list, which is a special object usually called nil.
#A cons cell whose car is the first element of the list and whose cdr is a list containing the rest of the elements.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:

#Cons 3 onto nil, the empty list
#Cons 2 onto the result
#Cons 1 onto the result

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, 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:

(5 1 2 3)

Another useful list procedure is append, which concatenates two existing lists (i.e. combines two lists into a single list).

Trees

Binary trees that only store data in their leaves, are also easily constructed with cons. For example, the code:

(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 conversation

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 like crazy.

Not fundamental

Since 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.

Trivia

Under 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 [http://www.dvp.nu/ordlista.php] .

See also

* Lisp (programming language)
* CAR and CDR
* Constructor (computer science)
* Algebraic data type


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • consœur — [ kɔ̃sɶr ] n. f. • 1342 « femme membre d une confrérie »; de sœur, d apr. confrère ♦ Femme appartenant à une profession libérale, un corps constitué, considérée par rapport aux autres membres (et notamment aux autres femmes) de cette profession.… …   Encyclopédie Universelle

  • CONS — CONS, Connection Oriented Network Service, is one of the two OSI network layer protocols, the other being CLNS (Connectionless Network Service). It is basically X.25, with a few adjustments.Protocols providing CONSSome protocols that provide the… …   Wikipedia

  • CONS — Saltar a navegación, búsqueda CONS, es la abreviatura de Servicio de red orientado a conexión (en inglés Connection Oriented Network Service), también abreviado a veces con las siglas CO. El servicio se modeló basándose en el sistema telefónico.… …   Wikipedia Español

  • Cons — abbrev. 1. constable 2. Constitution 3. consul * * * …   Universalium

  • cons — abbrev. 1. consecrated 2. consolidated 3. consonant 4. constable 5. constitutional 6. construction 7. consul * * * …   Universalium

  • Cons. — Cons.     † Catholic Encyclopedia ► Ecclesiastical Abbreviations     ► Abbreviation in general use, chiefly Ecclesiastical     Consecratio ( Consecration ) The Catholic Encyclopedia, Volume VIII. New York: Robert Appleton Company. Nihil Obstat …   Catholic encyclopedia

  • CONS — in nummis Constantini et Constantii: CONSA, in Iustini, Constantinopoli signata denotat, uti A. Monetarii index est, Car. du Fresue, ubi supra …   Hofmann J. Lexicon universale

  • cons. — cons. abbr. consiglio | consigliere …   Dizionario italiano

  • Cons — abbrev. 1. constable 2. Constitution 3. consul …   English World dictionary

  • cons — abbrev. 1. consecrated 2. consolidated 3. consonant 4. constable 5. constitutional 6. construction 7. consul …   English World dictionary

  • cons — This article is about the Lisp programming function. For other uses, see Cons (disambiguation). In computer programming, cons (  /ˈkɒ …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”