Professional Documents
Culture Documents
Exp 8 Prolog
Exp 8 Prolog
move(M,Z,Y,X).
1. TOWER OF HANOI Here is what happens when Prolog solves the
move(1,X,Y,_) :- case N=3.
write('Move top disk from '), ?- move(3,left,right,center).
write(X), Move top disk from left to right
write(' to '), Move top disk from left to center
write(Y), Move top disk from right to center
nl. Move top disk from left to right
move(N,X,Y,Z) :- Move top disk from center to left
N>1, Move top disk from center to right
M is N-1, Move top disk from left to right
move(M,X,Z,Y),
move(1,X,Y,_), yes
2. FIBONACCI
Predicates: Output:
fib(0,1). 1 ?- fib(5,Y).
fib(1,1). Y=8
fib(X,Y):-X>1,X1 is X-1,X2 is X-
2,fib(X1,W),fib(X2,Z),Y is W+Z.
3. FACTORIAL F is N * F1.
factorial(0,1).
?- factorial(3,6).
factorial(N,F) :- yes
N>0, ?- factorial(5,2).
N1 is N-1, no
factorial(N1,F1),
4. RELATIONS father(amod,sachin).
domains uncle(pramod,sachin).
X, Y, sachin, dhara, amod, geeta, harishchandra, aunt(mamta,sachin).
laxmi, pramod, mamta=symbol grandfather(harishchandra,sachin).
predicates grandmother(laxmi,sachin).
parent(X,Y) parent(X,Y):-mother(X,Y).
mother(X,Y) parent(X,Y):-father(X,Y).
father(X,Y) //Output:
uncle(X,Y) Goal: father(X,sachin)
aunt(X,Y) X=amod
grandfather(X,Y) 1 Solution
grandmother(X,Y) Goal: mother(X,sachin)
sister(X,Y) X=geeta
clauses 1 Solution
sister(dhara,sachin). Goal: grandfather(X,sachin)
mother(geeta,sachin). X=harishchandra
1 Solution X=geeta
Goal: grandmother(X,sachin) 2 Solutions
X=laxmi Goal: uncle(X,sachin)
1 Solution X=pramod
Goal: sister(X,sachin) 1 Solution
X=dhara Goal: aunt(X,sachin)
1 Solution X=mamta
Goal: parent(X,sachin) 1 Solution
X=amod
Output:
Goal:can_reach(X,Y).