Prolog Examples

LISTS in Prolog Lists in Prolog are signified by the symbols [ and ]. Thus a list of constants could be written in the form:

[a, b, c, d]

A list of variables is similarly:

[A, B, X, Y]

Like LISP the language Prolog has a means of identifying the head and tail of a list. Any list which contains the vertical bar | identifies the item to the left as the head and the list to the right as the tail. Generally this used in parameters of predicates to pick out the head or tail of a list.


Let us define the member function that we defined earlier in LISP:

The pseudocode is written in terms of predicates:

X is a member of the list if it is the head item of the list; Otherwise check if X is a member of the tail.

That is:

member(X, [X|Y]).
member(X, [H|L]) :- member(X,L).

Thus member(a, [a, b, c]) would return yes by the first rule. member(a, [b, c, d, a]) would be found by the successive application of the rules above, successively reducing the size of the list until a appears in the head position.


CS1104 Main Page

Last updated 2001/04/06
© J.A.N. Lee, 2001.