In

_{1}, ''l''_{1}) = cons (''e''_{2}, ''l''_{2}) if ''e''_{1} = ''e''_{2} and ''l''_{1} = ''l''_{2}
Note that first (nil ()) and rest (nil ()) are not defined.
These axioms are equivalent to those of the abstract stack data type.
In

^{*} rather than ''L'' to represent monomorphic lists with elements of type ''E''):
:$\backslash text\backslash colon\; A\; \backslash to\; A^\; =\; a\; \backslash mapsto\; \backslash text\; \backslash ,\; a\; \backslash ,\; \backslash text$
:$\backslash text\backslash colon\; A^\; \backslash to\; (A\; \backslash to\; B^)\; \backslash to\; B^\; =\; l\; \backslash mapsto\; f\; \backslash mapsto\; \backslash begin\; \backslash text\; \&\; \backslash text\; \backslash \; l\; =\; \backslash text\backslash \backslash \; \backslash text\; \backslash ,\; (f\; \backslash ,\; a)\; \backslash ,\; (\backslash text\; \backslash ,\; l\text{'}\; \backslash ,\; f)\; \&\; \backslash text\; \backslash \; l\; =\; \backslash text\; \backslash ,\; a\; \backslash ,\; l\text{'}\; \backslash end$
where ''append'' is defined as:
:$\backslash text\backslash colon\; A^\; \backslash to\; A^\; \backslash to\; A^\; =\; l\_1\; \backslash mapsto\; l\_2\; \backslash mapsto\; \backslash begin\; l\_2\; \&\; \backslash text\; \backslash \; l\_1\; =\; \backslash text\; \backslash \backslash \; \backslash text\; \backslash ,\; a\; \backslash ,\; (\backslash text\; \backslash ,\; l\_1\text{'}\; \backslash ,\; l\_2)\; \&\; \backslash text\; \backslash \; l\_1\; =\; \backslash text\; \backslash ,\; a\; \backslash ,\; l\_1\text{'}\; \backslash end$
Alternatively, the monad may be defined in terms of operations ''return'', ''fmap'' and ''join'', with:
:$\backslash text\; \backslash colon\; (A\; \backslash to\; B)\; \backslash to\; (A^\; \backslash to\; B^)\; =\; f\; \backslash mapsto\; l\; \backslash mapsto\; \backslash begin\; \backslash text\; \&\; \backslash text\; \backslash \; l\; =\; \backslash text\backslash \backslash \; \backslash text\; \backslash ,\; (f\; \backslash ,\; a)\; (\backslash text\; f\; \backslash ,\; l\text{'})\; \&\; \backslash text\; \backslash \; l\; =\; \backslash text\; \backslash ,\; a\; \backslash ,\; l\text{'}\; \backslash end$
:$\backslash text\; \backslash colon\; ^\; \backslash to\; A^\; =\; l\; \backslash mapsto\; \backslash begin\; \backslash text\; \&\; \backslash text\; \backslash \; l\; =\; \backslash text\backslash \backslash \; \backslash text\; \backslash ,\; a\; \backslash ,\; (\backslash text\; \backslash ,\; l\text{'})\; \&\; \backslash text\; \backslash \; l\; =\; \backslash text\; \backslash ,\; a\; \backslash ,\; l\text{'}\; \backslash end$
Note that ''fmap'', ''join'', ''append'' and ''bind'' are well-defined, since they're applied to progressively deeper arguments at each recursive call.
The list type is an additive monad, with ''nil'' as the monadic zero and ''append'' as monadic sum.
Lists form a

computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , , and . Computer science ...

, a list or sequence is an abstract data type
In computer science, an abstract data type (ADT) is a mathematical model for data types. An abstract data type is defined by its behavior (Semantics (computer science), semantics) from the point of view of a ''user'', of the data, specifically in ...

that represents a finite number of order
Order or ORDER or Orders may refer to:
* Orderliness
Orderliness is associated with other qualities such as cleanliness
Cleanliness is both the abstract state of being clean and free from germs, dirt, trash, or waste, and the habit of achieving a ...

ed value
Value or values may refer to:
* Value (ethics) it may be described as treating actions themselves as abstract objects, putting value to them
** Values (Western philosophy) expands the notion of value beyond that of ethics, but limited to Western s ...

s, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical
Mathematics (from Greek
Greek may refer to:
Greece
Anything of, from, or related to Greece
Greece ( el, Ελλάδα, , ), officially the Hellenic Republic, is a country located in Southeast Europe. Its population is approximately 10.7 ...

concept of a tuple
In mathematics
Mathematics (from Ancient Greek, Greek: ) includes the study of such topics as quantity (number theory), mathematical structure, structure (algebra), space (geometry), and calculus, change (mathematical analysis, analysis). ...

or finite sequence
In , a sequence is an enumerated collection of in which repetitions are allowed and matters. Like a , it contains (also called ''elements'', or ''terms''). The number of elements (possibly infinite) is called the ''length'' of the sequence. Unl ...

; the (potentially) infinite analog of a list is a stream
A stream is a body of water with surface water flowing within the stream bed, bed and Bank (geography), banks of a Channel (geography), channel. The flow of a stream is controlled by three inputs – surface water, subsurface water and ground ...

. Lists are a basic example of container
A container is any receptacle or enclosure for holding a product used in storage, packaging, and transportation, including shipping.
Things kept inside of a container are protected by being inside of its structure. The term is most frequently ...

s, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
The name list is also used for several concrete data structure
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of ...

s that can be used to implement abstract lists, especially linked list
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , ...

s and array
ARRAY, also known as ARRAY Now, is an independent distribution company launched by film maker and former publicist Ava DuVernay
Ava Marie DuVernay (; born August 24, 1972) is an American filmmaker. She won the directing award in the U.S. dram ...

s. In some contexts, such as in Lisp
Lisp (historically LISP) is a family of programming language
A programming language is a formal language
In logic, mathematics, computer science, and linguistics, a formal language consists of string (computer science), words whose symbo ...

programming, the term list may refer specifically to a linked list rather than an array. In class-based programming
Class-based programming, or more commonly class-orientation, is a style of object-oriented programming
Object-oriented programming (OOP) is a programming paradigm
Programming paradigms are a way to classify programming languages based o ...

, lists are usually provided as instances of subclasses of a generic "list" class, and traversed via separate iterator
In computer programming, an iterator is an object (computing), object that enables a programmer to traverse a Container (data structure), container, particularly List (abstract data type), lists. Various types of iterators are often provided via a ...

s.
Many programming language
A programming language is a formal language
In logic, mathematics, computer science, and linguistics, a formal language consists of string (computer science), words whose symbol (formal), letters are taken from an alphabet (computer science) ...

s provide support for list data types, and have special syntax and semantics for lists and list operations. A list can often be constructed by writing the items in sequence, separated by comma
The comma is a punctuation
Punctuation (or sometimes interpunction) is the use of spacing, conventional signs (called punctuation marks), and certain typographical devices as aids to the understanding and correct reading of written text, wh ...

s, semicolon
The semicolon or semi-colon is a symbol commonly used as orthographic punctuation
Punctuation (or sometimes interpunction) is the use of spacing, conventional signs (called punctuation marks), and certain typographical devices as aids to ...

s, and/or space
Space is the boundless three-dimensional
Three-dimensional space (also: 3-space or, rarely, tri-dimensional space) is a geometric setting in which three values (called parameter
A parameter (from the Ancient Greek language, Ancient Gre ...

s, within a pair of delimiters such as parentheses
A bracket is either of two tall fore- or back-facing punctuation
Punctuation (or sometimes interpunction) is the use of spacing, conventional signs (called punctuation marks), and certain typographical devices as aids to the understanding ...

'()', bracket
A bracket is either of two tall fore- or back-facing punctuation
Punctuation (or sometimes interpunction) is the use of spacing, conventional signs (called punctuation marks), and certain typographical devices as aids to the understanding ...

s '[]', brace (punctuation), braces '', or angle brackets '<>'. Some languages may allow list types to be array index, indexed or array slicing, sliced like array data type, array types, in which case the data type is more accurately described as an array.
In type theory
In mathematics
Mathematics (from Greek: ) includes the study of such topics as numbers ( and ), formulas and related structures (), shapes and spaces in which they are contained (), and quantities and their changes ( and ). There is no gene ...

and functional programming
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , ...

, abstract lists are usually defined inductively by two operations: ''nil'' that yields the empty list, and ''cons'', which adds an item at the beginning of a list.
Operations

Implementation of the list data structure may provide some of the followingoperations
Operation or Operations may refer to:
Science and technology
* Surgical operation
Surgery ''cheirourgikē'' (composed of χείρ, "hand", and ἔργον, "work"), via la, chirurgiae, meaning "hand work". is a medical or dental specialty that ...

:
* a constructor for creating an empty list;
* an operation for testing whether or not a list is empty;
* an operation for prepending an entity to a list
* an operation for appending an entity to a list
* an operation for determining the first component (or the "head") of a list
* an operation for referring to the list consisting of all the components of a list except for its first (this is called the "tail" of the list.)
* an operation for accessing the element at a given index.
Implementations

Lists are typically implemented either aslinked list
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , ...

s (either singly or doubly linked) or as arrays
ARRAY, also known as ARRAY Now, is an independent distribution company launched by film maker and former publicist Ava DuVernay
Ava Marie DuVernay (; born August 24, 1972) is an American filmmaker. She won the directing award in the U.S. drama ...

, usually variable length or dynamic array
In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libra ...

s.
The standard way of implementing lists, originating with the programming language Lisp
Lisp (historically LISP) is a family of programming language
A programming language is a formal language
In logic, mathematics, computer science, and linguistics, a formal language consists of string (computer science), words whose symbo ...

, is to have each element of the list contain both its value and a pointer indicating the location of the next element in the list. This results in either a linked list
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , ...

or a tree
In botany, a tree is a perennial plant with an elongated Plant stem, stem, or trunk (botany), trunk, supporting branches and leaves in most species. In some usages, the definition of a tree may be narrower, including only wood plants with se ...

, depending on whether the list has nested sublists. Some older Lisp implementations (such as the Lisp implementation of the Symbolics
Symbolics is a defunct computer manufacturer Symbolics, Inc., and a privately held company that acquired the assets of the former company and continues to sell and maintain the Open Genera Lisp (programming language), Lisp system and the Macsy ...

3600) also supported "compressed lists" (using CDR coding
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , , a ...

) which had a special internal representation (invisible to the user). Lists can be manipulated using iteration
Iteration is the repetition of a process in order to generate a (possibly unbounded) sequence of outcomes. Each repetition of the process is a single iteration, and the outcome of each iteration is then the starting point of the next iteration. ...

or recursion
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics
Linguistics is the scientific study of language, meaning tha ...

. The former is often preferred in imperative programming languages, while the latter is the norm in functional language
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of Alg ...

s.
Lists can be implemented as self-balancing binary search tree
251px, The same tree after being height-balanced; the average path effort decreased to 3.00 node accesses
In computer science, a self-balancing (or height-balanced) binary search tree is any node (computer science), node-based binary search tree ...

s holding index-value pairs, providing equal-time access to any element (e.g. all residing in the fringe, and internal nodes storing the right-most child's index, used to guide the search), taking the time logarithmic in the list's size, but as long as it doesn't change much will provide the illusion of random access
Random access (more precisely and more generally called direct access) is the ability to access an arbitrary element of a sequence in equal time or any datum from a population of addressable elements roughly as easily and efficiently as any othe ...

and enable swap, prefix and append operations in logarithmic time as well.
Programming language support

Some languages do not offer a listdata structure
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of ...

, but offer the use of associative array
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of , ...

s or some kind of table to emulate lists. For example, Lua provides tables. Although Lua stores lists that have numerical indices as arrays internally, they still appear as dictionaries.
In Lisp
Lisp (historically LISP) is a family of programming language
A programming language is a formal language
In logic, mathematics, computer science, and linguistics, a formal language consists of string (computer science), words whose symbo ...

, lists are the fundamental data type and can represent both program code and data. In most dialects, the list of the first three prime numbers could be written as `(list 2 3 5)`

. In several dialects of Lisp, including Scheme, a list is a collection of pairs, consisting of a value and a pointer to the next pair (or null value), making a singly linked list.
Applications

As the name implies, lists can be used to store a list of elements. However, unlike in traditionalarrays
ARRAY, also known as ARRAY Now, is an independent distribution company launched by film maker and former publicist Ava DuVernay
Ava Marie DuVernay (; born August 24, 1972) is an American filmmaker. She won the directing award in the U.S. drama ...

, lists can expand and shrink, and are stored dynamically in memory.
In computing, lists are easier to implement than sets. A finite set in the mathematical sense can be realized as a list with additional restrictions; that is, duplicate elements are disallowed and order is irrelevant. Sorting the list speeds up determining if a given item is already in the set, but in order to ensure the order, it requires more time to add new entry to the list. In efficient implementations, however, sets are implemented using self-balancing binary search tree
251px, The same tree after being height-balanced; the average path effort decreased to 3.00 node accesses
In computer science, a self-balancing (or height-balanced) binary search tree is any node (computer science), node-based binary search tree ...

s or hash table
In computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and s ...

s, rather than a list.
Lists also form the basis for other abstract data types
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of Algo ...

including the queue__NOTOC__
Queue () may refer to:
* Queue area, or queue, a line or area where people wait for goods or services
Arts, entertainment, and media
*''ACM Queue'', a computer magazine
* The Queue (publishing company), an American publishing company
* ...

, the stack, and their variations.
Abstract definition

The abstract list type ''L'' with elements of some type ''E'' (a monomorphic list) is defined by the following functions: :nil: () → ''L'' :cons: ''E'' × ''L'' → ''L'' :first: ''L'' → ''E'' :rest: ''L'' → ''L'' with the axioms :first (cons (''e'', ''l'')) = ''e'' :rest (cons (''e'', ''l'')) = ''l'' for any element ''e'' and any list ''l''. It is implicit that :cons (''e'', ''l'') ≠ ''l'' :cons (''e'', ''l'') ≠ ''e'' :cons (''e''type theory
In mathematics
Mathematics (from Greek: ) includes the study of such topics as numbers ( and ), formulas and related structures (), shapes and spaces in which they are contained (), and quantities and their changes ( and ). There is no gene ...

, the above definition is more simply regarded as an inductive type
In type theory
In mathematics, logic, and computer science, a type system is a formal system in which every term has a "type" which defines its meaning and the operations that may be performed on it. Type theory is the academic study of type syst ...

defined in terms of constructors: ''nil'' and ''cons''. In algebraic terms, this can be represented as the transformation 1 + ''E'' × ''L'' → ''L''. ''first'' and ''rest'' are then obtained by pattern matching
In computer science
Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application.
Computer science is the study of Algor ...

on the ''cons'' constructor and separately handling the ''nil'' case.
The list monad

The list type forms amonad
Monad may refer to:
Philosophy
* Monad (philosophy)
Monad (from Greek
Greek may refer to:
Greece
Anything of, from, or related to Greece
Greece ( el, Ελλάδα, , ), officially the Hellenic Republic, is a country located in Southeas ...

with the following functions (using ''E''monoid
In abstract algebra
In algebra, which is a broad division of mathematics, abstract algebra (occasionally called modern algebra) is the study of algebraic structures. Algebraic structures include group (mathematics), groups, ring (mathematic ...

under the ''append'' operation. The identity element of the monoid is the empty list, ''nil''. In fact, this is the free monoidIn abstract algebra, the free monoid on a Set (mathematics), set is the monoid whose elements are all the finite sequences (or strings) of zero or more elements from that set, with string concatenation as the monoid operation and with the unique sequ ...

over the set of list elements.
See also

*Array
ARRAY, also known as ARRAY Now, is an independent distribution company launched by film maker and former publicist Ava DuVernay
Ava Marie DuVernay (; born August 24, 1972) is an American filmmaker. She won the directing award in the U.S. dram ...

*Queue__NOTOC__
Queue () may refer to:
* Queue area, or queue, a line or area where people wait for goods or services
Arts, entertainment, and media
*''ACM Queue'', a computer magazine
* The Queue (publishing company), an American publishing company
* ...

* Set
* Stack
*Stream
A stream is a body of water
(Lysefjord) in Norway
Norway, officially the Kingdom of Norway,Names in the official and recognised languages: Bokmål
Bokmål (, ; literally "book tongue") is an official written standard for the No ...

References

{{DEFAULTSORT:List (Computing) Data types Composite data types Abstract data types