Lists and strings

[] is the empty list; [a, 2+2, [5]] is the list containing the three elements a, 2+2, and [5]. [Head | Tail] is the list whose first element is Head and whose tail (list of remaining elements) is Tail. [a, X, c | Tail] is the list whose first three elements are a, X, and c, and whose remaining elements are given by the list Tail. Only one term may follow the ``|'': [a | X, Y] and [a | X | Y] are syntactic nonsense.

Unification can be performed on lists:

	[a, b, c] = [Head | Tail].	/* a = Head, [b, c] = Tail. */
	[a, b] = [A, B | T].	/* a = A, b = B, [] = Tail. */
	[a, B | C] = [X | Y].	/* a = X, [B | C] = Y. */
In most (but not all) Prolog systems, the list notation is syntactic sugar for the '.' functor, with the equivalence: '.'(Head, Tail) = [Head | Tail].

Two useful predicates are member/2, which succeeds when its first argument is a member of the list that is its second argument, and append/3, which is true when the third argument is the list formed by appending the first two lists. Neither is predefined. Definitions are:

	member(A, [A | _]).
	member(A, [_ | B]) :- member(A, B).

	append([A | B], C, [A | D]) :- append(B, C, D).
	append([], A, A).
The operator ``=..'', pronounced ``univ,'' succeeds when its left argument is a structure and its right argument is the corresponding list [Functor | Args].

Example: mother(M, bill) =.. [mother, M, bill].

A double-quoted character string is syntactic sugar for a list of the ASCII codes for those characters.

Example: "abc" = [97,98,99].

The predicate name/2 succeeds if its first argument is the atom formed from the string that is its second argument.

Example: name(abc, "abc").

Previous page
Table of contents
Next page


Copyright © 1995 by David Matuszek
All rights reserved.
Last updated July 15, 1995