Information Technology Reference
In-Depth Information
7
Lists in Prolog
Before moving on to consider bigger and better varieties of thinking, this chapter
brings the programming skills learned so far up to the next level.
The Prolog symbols in previous chapters were single atomic units: constants, vari-
ables, numbers. This chapter considers larger symbolic structures called lists, and some
predicates that assemble them and take them apart.
Lists are essential to the sorts of thinking studied in the chapters to follow.
Chapter 8, for example, deals with thinking about sentences and phrases of English,
and these end up being lists of words . Chapter 9 surveys thinking about plans to
achieve goals, and these end up being lists of actions to perform.
The first section of this chapter introduces lists and the two notations in Prolog that
are used for them. Section 2 considers how to write the clauses for predicates that
take lists as arguments, allowing properties over groups of objects to be expressed
very succinctly. Section 3 explains how to use two very useful built-in list predicates,
member and append , and reformulates in a clear and concise way the blocks-world
program from section 4.2.
7.1 Lists
A problem that comes up again and again in Prolog is writing clauses for predicates
that are very similar except for the number of arguments they take. Consider, for
example, the predicates used in chapter 5 to ensure that individuals in a group were
all distinct:
uniq_person3(X,Y,Z) :- ...
% For three people
uniq_person5(X,Y,Z,U,V) :- ...
% For five people
The uniq_digits predicate in figure 5.8 handles eight digits and has
(
×
)
/2
=
28
inequalities in it. If there were eighteen people, one would need a predicate like this:
uniq_person18(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R) :- ...
8
7
 
 
Search WWH ::




Custom Search