Information Technology Reference
In-Depth Information
(
×
)
=
where the body would contain
153
inequalities. It would clearly be
much better to be able to write just one predicate, like this:
uniq_people(L) :- ...
% For any number of people
This predicate would have to work for any
collection
L
of people, no matter how big
or small. Prolog provides such a collection, and it is called a list.
A Prolog
list
is a sequence of objects that are called its
elements
. Here are some
examples:
18
17
/
2
[anna,karenina]
A two-element list whose first element is the constant
anna
and whose second
element is the constant
karenina
.
[john,paul,george,ringo,zeppo]
A five-element list.
[1,2,3,3,2,1]
Lists may have repeated elements.
[]
A list with no elements: the
empty list
.
Lists may contain other lists as elements:
[[john,23],[mary,14],[hello]]
A list whose three elements are also lists.
[8,john,['199y',john]]
Another three-element list.
[[]]
A one-element list whose single element is the empty list.
Note that a one-element list is different from the element itself:
[anna]
is different from
anna
;
[[]]
is different from
[]
.
The first element of a nonempty list is called the
head
of the list. The list that is formed
by removing that first element (and the comma that follows it, if there is one) is called
the
tail
of the list. Here are some examples:
[a,b,c,d]
has head
a
and tail
[b,c,d]
;
[[a],b,[c]]
has head
[a]
and tail
[b,[c]]
;
[a,[b,c]]
has head
a
and tail
[[b,c]]
;