CS359 Exam Answer
CS359 Exam Answer
3-Predicate max/4:
max(X, Y, Z, X):- X > Y, X > Z, !.
max(X, Y, Z, Y):- Y > X, Y > Z, !.
max(X, Y, Z, Z):- Z > X, Z > Y.
Page 1 of 4
descendant(X, Y) :- child(X, Z), descendant(Z, Y).
a) Write a query to get the answer of each question from the given database, and show
the answer(s):
(i) Find the descendants of harry.
(ii) Find all children.
b) Show all answers of the following queries:
(i) ?- child(X, Y).
(ii) ?- descendant(helen, Y).
(iii) ?- descendant(X, tom).
(iv) ?- descendant(ed, tom).
2- Correct the errors in the following Prolog clauses:
a)Length([2, 3, 1], N). c) s(X), t(Y) :- k(X,Y).
b)write(Hello Friend). d) fun(N,M1):- fun(N - 1,M2), M1 is N * M2.
a) A query to get the answer of each question from the given database,
with the answer(s):
(i) Find the descendants of (ii) Find all children.
harry. ?- child(C, _).
?- descendant(X, harry). C = ed ;
X = helen ; C = george ;
X = ed ; C = helen ;
No C = mary ;
C = harry ;
No
b) The answers of the given queries:
(i) ?- child(X, Y). (ii) ?- descendant(helen, Y).
X = ed Y = harry ;
Y = helen ; Y = tom ;
X = george No
Y = mary ; (iii) descendant(X, tom).
X = helen X = mary ;
Y = harry ; X = harry ;
X = mary X = ed ;
Y = tom ; X = george ;
X = harry X = helen ;
Y = tom ; No
No (iv) ?- descendant(ed, tom).
Yes
2-Correct the errors in the following Prolog clauses:
a)Length([2, 3, 1], N). c) s(X), t(Y) :- k(X,Y).
length([2, 3, 1], N). k(X,Y):- s(X), t(Y).
b)write(Hello Friend). d) fun(N,M1):- fun(N - 1,M2), M1 is N *
write('Hello Friend'). M2.
fun(N,M1):- N1 is N – 1, fun(N1,M2),
M1 is N * M2.
Page 2 of 4
III) 1-Consider the following facts that represent somekinds of animals (mammals and birds):
mammal(lion). bird(parrot).
mammal(elephant). bird(owl).
mammal(zebra). bird(penguin).
mammal(wolf). bird(goose).
Write a query to retrieve all birds
2-Using the search tree show all answers for the query a(X) to the following program:
a(X):-b(X,Y), c(Y). b(2,3). c(2).
a(X):-c(X). b(3,3). c(4).
b(1,2). b(3,4).
1- ?-bird(X).
2- Using the search tree, all answers for the query a(X):
a(X)
?- a(X).
b(X,Y), c(Y) c(X) X=1;
X=3, Y=4 X=3;
X=1, Y=2 X=2 X=4
X=3, X=2;
X=2, Y=3 X=4;
c(2) c(3) Y=3 c(3) c(4) No
Fail Fail
Page 3 of 4
IV) 1-Using the predicates: male/1, female/1, and parent/2, write facts that describe
thefollowing information about a family:
Mary and Lisa are female.
Peter and Bob are male.
Lisa is the parent of Bob and Mary.
Peter is the parent of Lisa.
Then, usingthe predicates: male/1, female/1, and parent/2, write rules that describe the
relations: sister and grandfather.
Finally, write queries to retrieve the following information and show their answers:
a) Who is the parent of Bob?
b) Who is the grandfather of Mary?
c) Who is the sister of Bob?
d)AreMary and Bob the children of Lisa?
The facts:
female(mary). male(peter). parent(lisa,bob).
female(lisa). male(bob). parent(lisa,mary).
parent(peter,lisa).
The rules:
sister(X,Y) :- parent(Z, X), parent(Z, Y), X \== Y, female(X).
grandfather(X,Y) :- parent(X, Z), parent(Z, Y), male(X).
The queries:
a) Who is the parent of Bob? c) Who is the sister of Bob?
?- parent(X, bob). ?- sister(X, bob).
X = lisa; X = mary;
No No
b) Who is the grandfather of d) AreMary and Bob the children of
Mary? Lisa?
?- grandfather(X, mary). ?- parent(lisa, mary), parent(lisa,
X = peter; bob).
No Yes
Best wishes …
Dr. Osman Sadek
Page 4 of 4