10.4 Exercises

Exercise 10.1

Suppose we have the following database:

p(1).
p(2) :- !.
p(3).

Write all of Prolog's answers to the following queries:

?- p(X).
 
?- p(X),p(Y).
 
?- p(X),!,p(Y).

Exercise 10.2

First, explain what the following program does:

class(Number,positive) :- Number > 0.
class(0,zero).
class(Number, negative) :- Number < 0.

Second, improve it by adding green cuts.

Exercise 10.3

Without using cut, write a predicate split/3 that splits a list of integers into two lists: one containing the positive ones (and zero), the other containing the negative ones. For example:

    split([3,4,-5,-1,0,4,-9],P,N)

should return:

    P = [3,4,0,4]
 
    N = [-5,-1,-9].

Then improve this program, without changing its meaning, with the help of cut.


Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)