6. Lists

A list holds any number of terms in order. Both goals of this tutorial use lists a lot. The Game of Life keeps the living cells in a list, and the Markdown converter reads the text as a list of characters.

Writing lists

A list is written in square brackets. [] is the empty list.

[react, vite, rollup]
[1, 2, 3]
[]
[version(18, 2, 0), point(1, 2), [nested, list]]

Head and tail

The most important way to look at a list is [Head | Tail]. The head is the first item, and the tail is the list of the other items.

[only] has the head only and the tail []. The empty list has no head, so the last query fails.

A list is really a chain of pairs. [1, 2, 3] is the same as [1 | [2 | [3 | []]]]. That is why recursion works so well with lists. Handle the head, then handle the tail with the same rule.

Your own member

my_member(X, List) is true when X is in List.

my_member(X, [X | _]).
my_member(X, [_ | Tail]) :- my_member(X, Tail).
  • X is in the list if it is the head.
  • X is in the list if it is in the tail.

There is no clause for the empty list, because nothing is in it. The query just fails.

The second query uses the same two lines to go through the list, one item per answer. There is no loop in the code. Backtracking is the loop.

Prolog has this predicate built in, as member/2.

Your own append

my_append(A, B, C) is true when C is the list A followed by the list B.

my_append([], L, L).
my_append([H | T], L, [H | R]) :- my_append(T, L, R).
  • Appending an empty list to L gives L.
  • Appending [H | T] to L gives a list that starts with H, followed by T appended to L.

Append in every direction

Here is where Prolog shines. my_append/3 is a relation between three lists. It is not a function from two lists to one. So you can leave any of them unknown.

Press Next answer on the second query. Prolog gives all four ways to split the list. You wrote two lines to join lists, and you got a list splitter and a last item finder for free.

Prolog has this predicate built in too, as append/3.

Other list predicates

C#Prolog has these predicates for lists.

Predicate Meaning
member(X, L) X is in L.
memberchk(X, L) Like member/2, but stops at the first match.
append(A, B, C) C is A followed by B.
length(L, N) L has N items.
reverse(L, R) R is L in reverse order.
nth0(I, L, X), nth1(I, L, X) X is the item at index I, counting from 0 or 1.
last(L, X) X is the last item of L.
select(X, L, R) R is L without one X.
msort(L, S) S is L sorted.
sort(L, S) S is L sorted, without duplicates.

The last query makes a list of two unknown items. Prolog shows each unknown value as a name such as _G1.

Try it

  1. The box below has my_last/2, written with recursion instead of append/3. Compare it with my_member/2. Which clause is the base case?
  2. Write has_two(X, List), which is true when X appears in List twice or more. You can use select/3 and then member/2.
  3. Ask ?- select(X, [a, b, c], R). and look at all the answers.
An unhandled error has occurred. Reload 🗙