# Prolog/Lists
## Lists

Any language needs a way to handle collections of objects and prolog is no exception. A list in prolog can look like this:
The brackets are the beginning and the end of the list, and the commas separate the various elements. Here all the elements are atoms, but a list can contain all sorts of elements, and different types of element can occur in the same list. The following are examples of valid lists:
Most prolog code will not define a list explicitly, like these examples, but rather handle lists of any length, with many possible elements. To handle lists without knowing what's inside them or how long they are, you use the bar notation:
In this notation the variable Head represents the leftmost element of the list and the variable Tail represents the rest of the list represented as another list. A head can be anything, from a predicate to another list, but the tail is always another list.
Some default library rules are provided, e.g., length, reverse, append (also, these can be defined easily, as shown at the bottom of this page). To see how these work, try the following codes:
Prolog will answer 3.
Observe the difference here.
The following program shows how to use this.
Here, [H|T] is a list, H is the head, and T is the tail. For the query
Prolog will answer yes. For the query
Prolog will answer:
The program can even be used to 'create' a list:
here are some examples of lists, and what heads and tails they result in:
| List | Head | Tail |
| [a,b,c,d,e] | a | [b,c,d,e] |
| [a] | a | (an empty list) |
| [[a,b,c],a,b,[a,b]] | [a,b,c] | [a,b,[a,b]] |
Note that the empty list [ ] cannot be split up and therefore will not unify with [H|T].
Splitting up lists can be done with more than two heads:
This will split a list up into the first three elements, and the rest of the list. Note, however that this will fail if the list has fewer than three elements.
Now consider the following program:
This relation defines the last element of a list. It can be used as a program to find a list's last element:
First, there is the stop predicate, that says that the last element of a list with one element, is that element. The second line says that the last element (Elem) of a list [_|Tail] is the last element of its tail (Tail). Since we don't care about the head, we use the anonymous variable, _, for it.

## Examples
### The member predicate

Normally, you would use built in predicates for these list operations, instead of writing them yourself. Built in predicates are defined by your prolog implementation, but can be used in any program. These implementations are shown here to illustrate how to modify lists.
Member is a standard prolog built-in predicate. You use it like this:
Where List is any prolog list, and Element is any element in that list. The following, for instance, succeeds (returns 'Yes'):
This query:
Will return the following values for Element:
The member predicate is defined like this:

### The append predicate

The built-in predicate append/3 attaches a list to the back of another, in other words, it concatenates two lists. It's used like this:
Where Zs is Ys appended to Xs. The following succeed:
You can use it to append two lists:
Or to split a list into left and right parts,
You can even use it with three variables. (Try this for yourself to see what the result looks like).
The append predicate can be defined like this:
The first line simply unifies the last two lists, it succeeds for queries like: append(, [a,b,c], [a,b,c]). or append(, X, [e]). in which case it would bind X = [e]. The second line (the recursive clause) declares that, given append(A,B,C) the head of A is equal to the head of C, and appending the tail of A, with B, gives the tail of C.
Due to the nondeterminism of Prolog, append/3 has many uses. It can be used as another way to implement last/2 which was defined earlier.
Many more definitions are possible,

### Reversing a list

We will look at two ways to reverse a list, first of all the naive way is to simply keep taking the head off and appending it onto the end,
Executing it means that you traverse the list over and over again appending each time, A more efficient version can be created by taking the definition of append:
and changing it slightly,
then,
The strategy used in revappend is called an accumulating parameter.

## Exercises

(1) The built in predicate length can be used to find the length of a list. For example:
How might this predicate be defined?

## Answers to Exercises

(1) As you probably guessed, we will use recursion.
Another solution which uses tail recursion optimisation and Prolog's arithmetic (And therefore uses less stack space):
prev:Variables next:Math, Functions and Equality