In computer programming, cons ( or /ˈkɒns/) is a fundamental function in most 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.
Welcome to CWAnswers
CWAnswers is your guide to the sprawling world wide web. The directory aims to provide a useful guide made by users. You can share your knowledge as well - simply sign up and edit your first entry. For questions just contact the team at support - at - cwanswers.com.
Weblinks for Cons
Top 10 for Cons
Things about Cons you find nowhere else.
Select content modules
In computer programming, cons ( or /ˈkɒns/) is a fundamental function in most 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, which adds an element to the beginning of a list.)
Use
Although cons cells can be used to hold 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:
Note the dot between 1 and 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
caris the first element of the list and whosecdris 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:
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).


























