Professional Documents
Culture Documents
1. State which of the following pairs of Prolog expressions unify and (if they do) .what the result
of their unification is.
(i) male(X) male(phil)
(ii) parent(X, chas) parent(liz, Y)
(iii) parent(X, chas) parent(liz, X)
(iv) father(mother(bill)) father(Jane)
(v) Mary mother(child(Mary))
(vi) loves(john, _) loves(_, Mary)
(vii) f(X, a(b,c)) f(d, a(Z, c))
(viii) f(X, a(b,c)) f(Z, a(Z, c))
(ix) f(c, a(b,c)) f(Z, a(Z, c)) u
(x) g(Z, f(A, 17, B), A+B, 17) g(C, f(D, D, E), C, E)
(b) For each of the following Prolog unifications, state whether it will succeed, and if so,
what variable instantiation and/or variable sharing will be produced; if it will not succeed,
say (briefly) why.
2. 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.
2
3. Write a rule reverselist(X,Y) such that given list X, Y is the result of reversing X. Use
reverselist(X,Y) in queries.
4. Use cuts to write the rule delete_first(E, L, A), which deletes only the first occurrence of
element E from list L, producing answer A. For example:
Glasgow->Manchester=300 miles
Manchester->London=300 miles
London->Portsmouth=100 miles
Montreal->Toronto=300 miles
Cane->Paris=200 miles
Amsterdam->Paris=400 miles
The links are directional, so if A and B are only connected by a single arrow from A -> B, then
there is no way to get from B to A using a direct link.
a. Write out these links using a predicate road(X, Y) which is true if there is a road leading from
X to Y.
Note that the intended interpretation of the road predicate is explicitly that road(X, Y) means
a road from X to Y. Thus, road (a,b) says nothing about how to get from b to a.
b. Now write a rule for road link/2 that is true if there is a road leading from the first argument to
the second.
HINT: your rule will have to be recursive.
3
d. Now formulate the distances as a set of clauses using the predicate distance/3 that is true if
there is a direct road link from the first argument to the second with distance given by the third
argument (in miles).
7. Consider the digits: 1234567890. First remove one digit, then swap two adjacent digits and
then insert some number of plus signs between the digits. There is a way to do this so that
the answer to the resulting arithmetic expression is 2004!
Hints:
You should define the digits as a list in your code, i.e. put the fact:
digits([1,2,3,4,5,6,7,8,9,0]) in your program.