Homework#1 model Answer

advertisement
Princess Noura Bint Abdulrahman University
College of Computer Science and Information Systems
Computer Science Department
Submission is online
email. Any
email will be
CS461: Artificial Intelligence
Lab Homework #1
Homework #1 model answer
Name ………………………………………..
by a link not by
homework sent to the
ignored.
ID …………………….
Q1: Consider the following Prolog program:
likes( clive, tea ).
likes( clive, coffee ).
likes( brian, coffee ).
likes( diane, soda ).
1) What Prolog answers to these questions (“True”, “No solution”, or variables bindings – write all
possible bindings ).
a) ?-likes (brian,coffee).
True
b) ?-likes(A,soda).
A= diana
c) ?-likes(clive,X).
X= tea , X= coffee.
d) ?-likes(X,X).
False
e) ?-likes(brain, soda).
False
2) Formulate the following questions: (1)
a- Is there anything that brian and clive both like?
likes(brian,B), likes(clive,B).
b- What does diana like?
likes(dina,M)
Princess Noura Bint Abdulrahman University
College of Computer Science and Information Systems
Computer Science Department
CS461: Artificial Intelligence
Lab Homework #1
Q2: Which of the following are syntactically correct Prolog objects? What kind of objects are they? (atom ,
number , variable , structure)
a. Diana variable
b. Diana atom
c. 'Diana' atom
d. _diana variable
e. 'diana goes south' atom
f. goes (diana,south) structure
g. 45 number
h. 5(X,Y) incorrect
i. +(north, west) structure
j. three(black(cats)). Structure
Q3: Will the following matching operation succeed or fail? If they succeed, what are the resulting
instantiation of variables? (2.25)
a) point( A, B) = point( 1, 2)
b) point( A, B) = point( X, Y, Z)
c) plus( 2, 2) = 4.
d) +( 2, D) = +( E, 2).
e) triangle( point(-1,0), P2, P3) = triangle( P1, point(1,0), point(0,Y) )
f) parent (adam, liz) = X.
g) ?- point(A,B) = point(1,2).
A=1
B=2;
No
h) ?- point(A,B) = point(X,Y,Z).
No
i)
?- plus(2,2) = 4.
No
j) ?- +(2,D) = +(E,2).
D=2
E=2;
No
k) ?- triangle(point(-1,0),P2,P3) = triangle(P1,point(1,0),point(0,Y)).
P2 = point(1, 0)
P3 = point(0, Y)
P1 = point(-1, 0)
Princess Noura Bint Abdulrahman University
College of Computer Science and Information Systems
Computer Science Department
CS461: Artificial Intelligence
Lab Homework #1
No
Q4: Consider the following program: )
f( 1, one).
f( s(1), two).
f( s(s(1)), three).
f( s(s(s(X))), N) :- f( X, N).
How will Prolog answer the following questions? Whenever several answers are possible, give at least two.
1. ?- f( s(1), A).
A = two.
2. ?- f( s(s(1)), two).
False.
3. ?- f( s(s(s(s(s(s(1)))))), C).
C=one.
4. ?- f( D, three).
D = s(s(1)) ;
D = s(s(s(s(s(1)))))
5. ?- f(A,B).
A = 1,
B = one ;
A = s(1),
B = two ;
6. ?-f(s(X),1).
Error.
7. ?- f(1,N).
N=1.
Download