List manipulation
Lists may be used to store and manipulate sequences of items.
Prolog offers a special syntax to deal with lists.
$=P
\prologindex{list manipulation}
\prologindex{\emptylist}
\prologindex{\prologlist}
\prologindex{\compoundlist}
Examples of terms that denote lists are
& -- denoting the empty list, \\
& -- denoting the list with elements a, b and c, and \\
& -- denoting the list T with element H put in front.
\end{tabular}
Internally, non-empty lists may be thought to be represented as terms constructed
with the function-symbol cons.
We use to represent the empty list.
To put an element a in front of the empty list we write .
The list is represented by the term
.
Lists in Prolog are always ended by an empty list indicating an invisible
last element.
The special notation closely corresponds to the use of the constructor cons.
The one-element list may be written as .
And similarly, the list may be written as
or or .
The notation , which stands for a non-empty list with element H
in front, is very convenient for performing operations on a list
as illustrated by the clauses below.
$=[H|T][H|L]