ArticleslgStudy

mathematics

Cons

Cons is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cons rather than just read about it. In short: In computer programming, cons ( or ) is a fundamental function in most dialects of the Lisp programming language. cons constructs memory objects which hold two values or pointers to two values. These objects are referred to as (cons) cells, conses, non-atomic s-expressions ("NATSes"), or (cons) pairs.

Cons — main illustration
Cons — illustration

Key takeaways

  • Cons belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cons to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cons from memory before moving on to harder problems.

Reference excerpt

In computer programming, cons ( or ) is a fundamental function in most dialects of the Lisp programming language. cons constructs memory objects which hold two values or pointers to two values. These objects are referred to as (cons) cells, conses, non-atomic s-expressions ("NATSes"), 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, or contents of the address part of register), and a right half, referred to as the cdr (the second element, or contents of the decrement part of register). 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, Scala, F#, Lean, Rocq, and Elm or the : operator in Haskell, which adds an element to the beginning of a list.)

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

Ordered pairs 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)

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

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:

or its shorthand:

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:

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

See also Lisp (programming language) CAR and CDR Constructor (computer science) Algebraic data type Hash consing

References

External links SDRAW, Common Lisp code for drawing draws cons cell structures. From David S. Touretzky.

Worked examples

Example 1 — a first encounter with Cons

Start with the simplest possible case. Write down what Cons claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cons before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cons ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cons

In research
Cons appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cons in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cons is common in secondary-school and first-year university syllabi. It links to neighbouring topics Composite data types, Data types, Functional programming, so understanding it makes those chapters shorter.
In everyday life
Look for Cons outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Cons” →

How to study Cons in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cons means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cons out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cons in simple terms?

In computer programming, cons ( or ) is a fundamental function in most dialects of the Lisp programming language. cons constructs memory objects which hold two values or pointers to two values. These objects are referred to as (cons) cells, conses, non-atomic s-expressions ("NATSes"), or (cons) pai…

Why does Cons matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cons?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cons.

Tags

  • Composite data types
  • Data types
  • Functional programming
  • Lisp (programming language)

Keep exploring