0% found this document useful (0 votes)
15 views4 pages

CS359 Exam Answer

Uploaded by

osmaneg200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

CS359 Exam Answer

Uploaded by

osmaneg200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Faculty of Science

Computer Science Department


Final ExamofLogic Programming(CS359)
Program: CS, Math & CS, and Ph & CS Academic Year: 2018-2019
Grade: 3rd Semester: Spring 2019
Date: 13-05- 2019 Model Answer Time: 2 hours
Answer the following questions: (Total score: 40 Marks)
(10 marks for each question)
I) 1-Show the answer(s) of the following queries:
a) ?- X is 2**4, Y is X//3. b) ?- like(jane, X) = like(Y, ice_cream).
c) X is 2**3, Y is X mod 3. d) ?- [X|Y] = [[a, b], c, d, [e, f]].
e) ?- member(X, [a,b,c]), member(Y, [d,b, c]), !, X = Y.
2- Write a query that reads two numbers and displays their sum.
3- Write a predicate max/4 that returns the largest of three integers,
Examples:
?- max(3, 5, 1, X). ?- X is max(7, 2, 5).
X=5 X=7

1- The answer(s) of the given queries:


a) ?- X is 2**4, Y is X//3. c) ?-X is 2**3, Y is X mod 3.
X = 16 X=8
Y=5 Y=2
Yes Yes
b) ?- like(jane, X) = like(Y, ice_cream). d) ?- [X|Y] = [[a, b], c, d, [e, f]].
X = ice_cream X = [a, b]
Y = jane Y = [c, d, [e, f]]
Yes Yes
e) ?- member(X, [a,b,c]), member(Y, [d,b, c]), !, X = Y.
No
2- A query that reads two numbers and displays their sum.
?- read(X), read(Y), Z is X + Y, write(Z).

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.

II) 1- Consider the following database:


child(ed, helen). child(mary, tom).
child(george, mary). child(harry, tom).
child(helen, harry).
descendant(X, Y) :- child(X, 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy