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 computation, automation, an ...

, a list or sequence is an abstract data type that represents a finite number of ordered 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 mathematics, a sequence is an enumerated collection of objects in which repetitions are allowed and order theory, order matters. Like a Set (mathematics), set, it contains Element (mathematics), members (also called ''elements'', or ''terms''). ...

; 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
box.
File:Railroad car with container loads.jpg, A Flatcar#Spine car, spine car with a tank container and an open-top intermodal container, intermodal shipping container with canvas cover.
A container is any receptacle or enclosure for holding ...

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
Image:Hash table 3 1 1 0 1 0 0 SP.svg, 315px, A data structure known as a hash table.
In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a ...

s that can be used to implement abstract lists, especially linked list
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element pointer (computer programming), points to the next. It is a data structure consisting ...

s and array
ARRAY, also known as ARRAY Now, is an independent distribution company launched by film maker and former publicist Ava DuVernay in 2010 under the name African-American Film Festival Releasing Movement (AFFRM). In 2015 the company rebranded itself ...

s. In some contexts, such as in Lisp
A lisp is a speech impairment in which a person misarticulates sibilant
In phonetics
Phonetics is a branch of linguistics that studies how humans produce and perceive sounds, or in the case of sign languages, the equivalent aspects of sign. P ...

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 (OOP) in which inheritance (object-oriented programming), inheritance occurs via defining ''class (computer programming), classes'' of object (c ...

, lists are usually provided as instances of subclasses of a generic "list" class, and traversed via separate iterator
In computer programming
Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task. Programming involves tasks such as: analysis, generati ...

s.
Many programming language
A programming language is a formal language comprising a Instruction set architecture, set of instructions that produce various kinds of Input/output, output. Programming languages are used in computer programming to implement algorithms.
Most ...

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

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

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, logic, and computer science, a type theory is a formal system in which every "term" has a "type". A "type" in type theory has a role similar to a "type" in a programming language: it dictates the operations that can be performed on ...

and functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declara ...

, 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, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element pointer (computer programming), points to the next. It is a data structure consisting ...

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 in 2010 under the name African-American Film Festival Releasing Movement (AFFRM). In 2015 the company rebranded itself ...

, usually variable length or dynamic 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 Algorit ...

s.
The standard way of implementing lists, originating with the programming language Lisp
A lisp is a speech impairment in which a person misarticulates sibilant
In phonetics
Phonetics is a branch of linguistics that studies how humans produce and perceive sounds, or in the case of sign languages, the equivalent aspects of sign. P ...

, 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, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element pointer (computer programming), points to the next. It is a data structure consisting ...

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
A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations automatically. Modern computers can perform generic sets of operations known as Computer program, ...

3600) also supported "compressed lists" (using CDR coding) 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 to logic. The most common application of recursion is in mathematics ...

. The former is often preferred in imperative programming languages, while the latter is the norm in functional language
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declara ...

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

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

Some languages do not offer a listdata structure
Image:Hash table 3 1 1 0 1 0 0 SP.svg, 315px, A data structure known as a hash table.
In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a ...

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

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
A lisp is a speech impairment in which a person misarticulates sibilant
In phonetics
Phonetics is a branch of linguistics that studies how humans produce and perceive sounds, or in the case of sign languages, the equivalent aspects of sign. P ...

, 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 in 2010 under the name African-American Film Festival Releasing Movement (AFFRM). In 2015 the company rebranded itself ...

, 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, logic, and computer science, a type theory is a formal system in which every "term" has a "type". A "type" in type theory has a role similar to a "type" in a programming language: it dictates the operations that can be performed on ...

, 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 in 2010 under the name African-American Film Festival Releasing Movement (AFFRM). In 2015 the company rebranded itself ...

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

References

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