109. Lists

Informally, a list of elements of a set $A$ consists of elements of $A$ arranged from first to last, with order and repetition mattering. We will write them using the same notation that we use for tuples. Thus $\langle 1, 4, 3, 3, 2\rangle$ is a list of elements of $\mathrm{N}$. It is not the same list as $\langle 1, 4, 3, 2\rangle$ or as $\langle 4, 1, 3, 3, 2\rangle$. A particular list is the empty list, denoted $\langle\rangle$.

We could have said that a list of elements of $A$ is just a tuple of elements of $A$. However, the specification for lists is different from that for tuples, so our formal treatment will start from scratch. The definition is recursive.

109.1 Definition: list

For any set $A$, a list of elements of $A$ is either the empty list $\langle\rangle$ or a nonempty list. A nonempty list of elements of $A$ has a head, which is an element of $A$, and a tail, which is a list of elements of $A$. The list with head $a$ and tail $\langle b_{1},\ldots ,b_{k}\rangle$ is denoted $\langle a,b_{1},\ldots ,b_{k}\rangle$. The list with head $a$ and empty tail is denoted $\langle a\rangle$. Every list of elements of $A$ is constructed by repeated application of this definition starting with the empty list.

109.1.1 Remark The head of a nonempty list is not a list, but the tail is a list. The empty list does not have a head or a tail.

109.1.2 Example $\langle\rangle, \langle 5\rangle, \langle 2, 1, 1,- 3\rangle$ and $\langle 3, 3, 3\rangle$ are all lists of elements of $\mathrm{Z}$ (lists of integers). The head of $\langle 2, 1, 1,- 3\rangle$ is 2 and the tail is $\langle 1, 1,- 3\rangle$. The head of $\langle 5\rangle$ is 5 and the tail is $\langle\rangle$.

109.2 Definition: set of lists

The set of all lists of elements of $A$ is denoted $A^{*}$. The set of all nonempty lists of elements of $A$ is denoted $A^{+}$.

109.2.1 Example Let $A$ be the English alphabet. Then the lists $\langle\rangle, \langle a,a,b\rangle$ and $\langle c,a,t,c,h\rangle$ are all elements of $A^{*}$. The list $\langle 2, 2\rangle$ is an element of $\mathrm{N}^{*}$, and $\langle c,a,t,c,h, 2, 2\rangle$ is an element of $(A\cup\mathrm{N})^{*}$ but not of $A^{*}$ or of $\mathrm{N}^{*}$.

109.2.2 Lists in Mathematica A list such as $\langle 1, 5, 3, 6\rangle$ in Mathematica is written $\{ 1, 5, 3, 6\}$.

109.3 The list constructor

Most concepts connected with lists are defined recursively using Definition 109.1. To make this easy, we introduce the list constructor function $\mathrm{cons}\colon S \times S^{*} \to S^{+}$ (note carefully the domain and codomain of this function), which is defined by requiring

$$\mathrm{cons}(a,\langle b_{1},b_{2},\ldots ,b_{n}\rangle ) =\langle a,b_{1},b_{2},\ldots ,b_{n}\rangle \tag{109.1}$$

Thus $\mathrm{cons}(c,\langle a,t,c,h\rangle ) =\langle c,a,t,c,h\rangle$ and $\mathrm{cons}(a,\langle\rangle ) =\langle a\rangle$.

109.4 Definition: length of a list

The length (of a list) of a list $L$ of elements of $S$ is denoted $|L|$ and is defined by

LL.1 $|\langle\rangle|= 0$.

LL.2 $|\mathrm{cons}(a,L )| = 1 +|L|$.

109.4.1 Example $|\langle c,a,t\rangle|= 3$, because, by repeatedly applying Rule (109.1), page 165, and LL.1 and LL.2, we have

$$\begin{array}{rcl} |\langle c,a,t\rangle| &=& |\mathrm{cons}(c,\langle a,t\rangle )| \\ &=& |\mathrm{cons}(c,\mathrm{cons}(a,\langle t\rangle ))| \\ &=& |\mathrm{cons}(c,\mathrm{cons}(a,\mathrm{cons}(t,\langle\rangle )))| \\ &=& 1 +|\mathrm{cons}(a,\mathrm{cons}(t,\langle\rangle ))| \\ &=& 1 + 1 +|\mathrm{cons}(t,\langle\rangle )| \\ &=& 1 + 1 + 1 +|\langle\rangle| \\ &=& 1 + 1 + 1 + 0 = 3 \end{array}$$

109.4.2 Remark It can be proved by induction on the length of a list that a list of length $k$ satisfies the specification for a $k$-tuple (Definition 36.2, page 50). Nevertheless, the recursive definition of list given above has provides a useful alternative approach to the idea which simplifies much of the theory of lists.

109.5 Concatenation

Informally, the concatenate of two lists is obtained by writing the entries of one and then the other in a single list. Concatenation is denoted by juxtaposition; thus $\langle 1, 4, 4\rangle\langle 2, 3\rangle =\langle 1, 4, 4, 2, 3\rangle$ and $\langle 3, 2, 2\rangle\langle\rangle =\langle 3, 2, 2\rangle$.

Again, we give a formal definition by induction.

109.6 Definition: concatenate of lists

The concatenate $LN$ of two lists $L$ and $N$ is defined recursively as follows:

CL.1 $\langle\rangle N =N$

CL.2 $\mathrm{cons}(a,L )N=\mathrm{cons}(a,LN)$.

109.6.1 Example

$$\begin{array}{rcl} \langle c,a,t\rangle\langle c,h\rangle &=& \mathrm{cons}(c,\langle a,t\rangle )\langle c,h\rangle \\ &=& \mathrm{cons}(c,\langle a,t\rangle\langle c,h\rangle) \\ &=& \mathrm{cons}(c,\mathrm{cons}(a,\langle t\rangle )\langle c,h\rangle) \\ &=& \mathrm{cons}(c,\mathrm{cons}(a,\langle t\rangle\langle c,h\rangle)) \\ &=& \mathrm{cons}(c,\mathrm{cons}(a,\mathrm{cons}(t,\langle\rangle )\langle c,h\rangle)) \\ &=& \mathrm{cons}(c,\mathrm{cons}(a,\mathrm{cons}(t,\langle\rangle\langle c,h\rangle))) \\ &=& \mathrm{cons}(c,\mathrm{cons}(a,\mathrm{cons}(t,\langle c,h\rangle))) \\ &=& \mathrm{cons}(c,\mathrm{cons}(a,\langle t,c,h\rangle)) \\ &=& \mathrm{cons}(c,\langle a,t,c,h\rangle) \\ &=& \langle c,a,t,c,h\rangle \end{array}$$

109.6.2 Remark Definition 109.6 implies that, for example, $\langle\rangle\langle c,a,t\rangle =\langle c,a,t\rangle$. We would expect that $\langle c,a,t\rangle\langle\rangle =\langle c,a,t\rangle$ as well. This can be proved by induction:

109.7 Theorem

For any list $L$, $L\langle\rangle =L$.

Proof If $L$ has length 0, that is, if $L =\langle\rangle$, then $L\langle\rangle =\langle\rangle\langle\rangle =\langle\rangle$ by CL.1. Otherwise, assume the theorem is true for lists of length $k$ and let $L$ have length $k+ 1$. Then $L=\mathrm{cons}(a,L')$ for some element $a$ and list $L'$ of length $k$, and

$$L\langle\rangle= \mathrm{cons}(a,L' )\langle\rangle= \mathrm{cons}(a,L'\langle\rangle) = \mathrm{cons}(a,L' ) =L$$

by CL.2 and the induction hypothesis.

109.8 Theorem

Concatenation is associative. Precisely, for any lists $L$, $M$ and $N$ , $(LM )N =L (MN)$ .

Proof This is also proved by induction on the length of $L$. If $L =\langle\rangle$, then $(LM )N = (\langle\rangle M )N =MN =\langle\rangle (MN)$ by CL.1 applied twice. Now assume that $L=\mathrm{cons}(a,L')$ and that $(L'M )N =L' (MN)$. Then

$$\begin{array}{rcll} (LM )N &=& \bigl(\mathrm{cons}(a,L' )M\bigr) N & \\ &=& \mathrm{cons}(a,L'M )N & \text{by CL.2} \\ &=& \mathrm{cons}(a, (L'M )N) & \text{by CL.2} \\ &=& \mathrm{cons}(a,L' (MN)) & \text{induction hypothesis} \\ &=& \mathrm{cons}(a,L' )(MN) & \text{by CL.2} \\ &=& L (MN) & \end{array}$$

109.8.1 Exercise Prove by induction that the length of the concatenate of two lists is the sum of the lengths of the lists. Use Definitions 109.4 and 109.6 explicitly.

109.8.2 Exercise Give an inductive definition of the last entry of a list. (Answer on page 249.)

109.8.3 Exercise Give an inductive definition of the maximum of a nonempty list of real numbers. It should satisfy $\max\langle 1, 3, 17, 2\rangle= 17$ and $\max\langle 5\rangle= 5$, for example.

109.8.4 Exercise Give an inductive definition of the sum of the entries of a list of real numbers. It should satisfy $\mathrm{SUM}\langle 3, 4, 2, 3\rangle= 12$ and $\mathrm{SUM}\langle 42\rangle= 42$. The sum of the empty list should be zero.

109.8.5 Exercise (hard) Prove that a list of length $k$ satisfies the specification for a tuple of length $k$ (Definition 36.2, page 50).

Pages 164–167 of the book (PDF).