AI Lab
AI Lab
INDEX
1 Study of Prolog.
Experiment Number: 1
c) QUERIES:
Given a database of facts and rules such as that above, we may make queries by typing after a
query a symbol’?’ statements such as:
?-parent(X,sam) Xann
?grandfather(X,Y)
X=jo, Y=sam
lOMoAR cPSD| 38464519
Experiment Number: 2
Clauses:
likes(ram
,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).
Output:
?-likes(ram,What).
What= mango
?-likes(Who,cindy).
Who= cindy
?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold.
lOMoAR cPSD| 38464519
Experiment Number: 03
Clauses:
c_to_f(C,F) :-
F is C * 9 / 5 + 32.
freezing(F) :-
F =< 32.
Output:-
lOMoAR cPSD| 38464519
Experiment Number: 4
Description:-Imagine a room containing a monkey, chair and some bananas. That has been
hanged from the centre of ceiling. If the monkey is clever enough he can reach the bananas
by placing the chair directly below the bananas and climb on the chair .The problem is to
prove the monkey can reach the bananas. The monkey wants it, but cannot jump high enough
from the floor. At the window of the room there is a box that the monkey can use. The
monkey can perform the following actions:-
1) Walk on the floor.
2) Climb the box.
3) Push the box around (if it is beside the box).
4) Grasp the banana if it is standing on the box directly under the banana.
Clauses:
on(floor,monkey).
on(floor,chair).
in(room,monkey).
in(room,chair).
in(room,banana).
at(ceiling,banana).
strong(monkey).
grasp(monkey).
climb(monkey,chair).
push(monkey,chair):-strong(monkey).
under(banana,chair):-push(monkey,chair).
canreach(banana,monkey):-at(floor,banana);at(ceiling,banana),under(banana,chair),
climb(monkey,chair).
canget(banana,monkey):-canreach(banana,monkey),grasp(monkey).
Output:
lOMoAR cPSD| 38464519
Experiment Number: 5
OBJECTIVE: WAP in turbo prolog for medical diagnosis and show the
advantage and disadvantage of green and red cuts.
Program:
Domains:
disease,indication=symbol
name-string
Predicates:
hypothesis(name,disease)
symptom(name,indication)
response(char)
go
goonce
clauses:
go:-
goonce
write("will you like to try again
(y/n)?"), response(Reply),
Reply='n'.
go.
goonce:-
write("what is the patient's name"),nl,
readln(Patient),
hypothesis(Patient,Disease),!,
write(Patient,"probably has",Disease),!,
goonce:-
write("sorry, i am not ina position to
diagnose"), write("the disease").
symptom(Patient,fever):-
write("does",Patient,"has a fever
(y/n)?"),nl, response(Reply),
Reply='y',nl.
symptom(Patient,rash):-
symptom(Patient,body_ache),
Symptom(Patient,runny_nose).
response(Reply):-
readchar(Reply),
write(Reply).
Output:
makewindow(1,7,7"Expert Medical Diagnosis",2,2,23,70), go.
lOMoAR cPSD| 38464519
Experiment Number: 6
Program:
Factorial:
factorial(0,1).
factorial(N,F) :-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.
Output:
Fibonacci:
fib(0, 0).
fib(X, Y) :- X > 0, fib(X, Y, _).
fib(1, 1, 0).
fib(X, Y1, Y2) :-
X>1,
X1 is X - 1,
fib(X1, Y2, Y3),
Y1 is Y2 + Y3.
Output:
lOMoAR cPSD| 38464519
Output:
lOMoAR cPSD| 38464519
Output:
lOMoAR cPSD| 38464519
Water Jug Problem: You are given two jugs, a 4lit one and a 3lit one, a pump which has
unlimited water which you can use to fill the jug, and the ground on which water may be poured.
Neither jug has any measuring markings on it. How can you get exactly 2lit of water in the 4lit
jug?
Program:
contains(_,Y,1):-start(4,Y).
contains(X,_,2):-start(X,3).
contains(_,Y,3):-start(0,Y).
contains(X,_,4):-start(X,0).
contains(X,Y,5):-N is Y-4+X, start(4,N).
contains(X,Y,6):-N is X-3+Y, start(N,3).
contains(X,Y,7):-N is X+Y, start(N,0).
contains(X,Y,8):-N is X+Y, start(0,N).
Output: