A list in Prolog is written as a comma-separated sequence of items, between square brackets. For example, `[1, 2, 3]` is a list.

The empty list is written `[]`.

A list with just a single item, say the number 7, is written `[7]`.

Frequently it is convenient to refer to a list by giving the first item, and a list consisting of the rest of the items. In this case, one writes the list as `[First | Rest]`. Note that `Rest` must be a list, while Head need not be.

We have expressed this here using variables, but this need not be so, for example, we could write `[1, 2, 3]` as:

• `[1 | [2, 3]]`
• `[1 | Rest]`, where `Rest` is bound to `[2, 3]`
• `[First | [2, 3]]`, where `First` is bound to `1`
• `[First | Rest]`, where `First` is bound to `1`, and `Rest` is bound to `[2, 3]`
• `[1, 2 | [3]]`
• `[1, 2, 3 | []]`
and many more possibilities.

You should always write your Prolog list in the most compact reasonable format. So for example, while `[X | []]` is the same list as `[X]`, the second version is much easier to read, so you should use it.

It is possible to have lists of lists, or lists some of whose members are themselves lists:

• `[[1, 2], [3, 4], [5, 6]]` could be used to represent a 3 × 2 matrix;
• `[[a, b], c(d, e, f), 1]` is a 3-element list whose first element is the list `[a, b]`, second element is the term `c(d, e, f)`, and whose final element is the number `1`.

Lists can also be expressed using a normal term syntax, using the built-in predicate name `.` - that is, a full stop or period. In this case, the empty list atom (`[]`) must be used to terminate the list. However, this approach is more cumbersome, and in practice people use the `[1, 2, 3]`- style syntax. Example:

```?- X = .(1, .(2, .(3, []))).
X = [1, 2, 3]
```