Download review questions prolog

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
AI Midterm Review
Fall 2020/2021
Q1. Which of the following are syntactically correct Prolog objects? If yes, identify
the types of object they are (atom, number, variable, structure). If not, use a “No” as
an answer.
Term
Type
1.
5xyz
No
2.
_
Varible
3.
-3.2
Number
4.
Tfile([a, b, c])
No
5.
+(John, Mary)
Structure
Q2. Show the variable instantiations (the values of X, Y and Z) if the matching
between the term in the first column and the term in the second column succeeds.
Use “No” otherwise.
Term1
Term2
Instantiations
1.
[f(a), f(a)]
[X | Y]
X= f(a), Y= [f(a)]
2.
[f(a), f(a), c]
[X, Y|Z]
X= f(a), Y= f(a), Z = [c]
3.
[g(c), b, c]
[g(X), Y, Z] X =c, Y=b, Z=c
4.
[g(Y), b, f(a)]
[X, Y, Z]
X=g(b), Y=b, Z= f(a)
5.
[a, [e, [d]], c]
[X,Y,Z]
X=a, Y=[e,[d]]. Z= c
6.
[1, 4, 6]
[ _ | [X| [Y]]] X=4, Y=6
7.
[a, b]
[ X | [Y | Z ] ] X=a, Y=b, Z= []
8.
[c, b, d]
[X, a | Y]
no
9.
f(g(Z, a), X)
f(Y, g(b, Z)) Y=g(Z,a), X=g(b,Z)
10.
p(f(X), g(b,
p(f(g(c)), g(X, no
Y))
a))
Q3. Consider the following program:
set_dif([],Y,[]).
set_dif([X|R],Y,Z) :- member(X,Y),set_dif(R,Y,Z),!.
set_dif([X|R],Y,[X|Z]) :- set_dif(R,Y,Z).
What is the output of the following?
? set_dif([1,2,3,4],[3,4,5,6],L).
L= [1,2]
Q4. Suppose you have the following prolog program:
fib(0,0).
fib(1,1).
fib(N,F):- N>1, N1 is N-1,
fib(N1,F1), N2 is N-2,
1
fib(N2, F2), F is F1+F2.
What is the output of the followings?
a) ? fib(-1,N)
no
b) fib(2,N).
N= 1
c) fib(6,N).
N= 8
Q5. Write a Prolog predicate split(List, Odds, Evens) that insert the following.
• Odds contains all the items in the odd positions of List
• Evens contains all the items in the even positions of List
Example: ? split([a,b,c,e,f,g,h],O, E).
O = [a,c,f,h], E = [b,e,g],
split([ ], [ ], [ ]).
split([X], [X],[]).
split([X ,Y |T ],[ X |T1], [Y |T2 ]):split(T ,T1,T2 ).
Q6. Write a prolog Program that read students grades in AI class. Keep reading until
a negative grade is read. Display the average of the class
Your program should display:
The average of the class = ###.##
:- dynamic stat/2.
stat(0,0).
do:write(‘Enter a grade [negative to stop]:’),
read(G),
process(G).
process(G):G < 0,
retract(stat(Sum, Count)),
Average is Sum / Count,
write(‘The average of the class =’),
write(Average), !.
process(G):G >= 0,
retract(stat(Sum, Count)),
Sum1 is Sum + G,
Count1 is Count + 1,
assert(stat(Sum1,Count1)),
nl,
do.
End of Review
2