ai-wm-review questions
ai-wm-review questions
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))
L= [1,2]
1
fib(N2, F2), F is F1+F2.
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