Professional Documents
Culture Documents
PROLOG
->(setq x 10)
10
-> x
10
-> (setq x (+ 3 5))
8
-> (setq x ‘(+ 3 5))
(+ 3 5)
-> y
Y
->(defun maximum3 (a b c)
(cond ((>a b) (cond ((> a c) a)
(t c)))
((>b c) b)
(t c)))
->MAXIMUM3
->(maximum3 20 30 25)
30
->(defun circle-area()
(terpri)
(princ “Please enter the radius: ”)
(setq radius (read))
(princ “The area of the circle is: ”)
(princ (*3.1416 radius radius))
(terpri)
CIRCLE-AREA
->(circle-area)
Please enter the radius: 4
The area of the circle is: 50.2656
4. LISP recursion program for finding factorial of a number.
(remove-null
(list
(fill-first state)
(fill-second state)
(pour-first-second state)
(pour-second-first state)
(empty-first state)
(empty-second state))))
(cond
((null x) nil)
(cond
(cond
(s (second-jug state)))
(cond
(mk-state 0 (+ f s)))
(mk-state (- (+ f s) 3) 3)))))
(s (second-jug state)))
(cond
(mk-state (+ f s) 0))
(mk-state 4 (- (+ f s) 4))))))
(cond
(cond
father(dashrath, ram).
father(ram, love).
mother(kaushalya, ram).
mother(seeta, love).
husband(dashrath, kaushalya).
husband(ram, seeta).
sister(seeta, urmila).
brother(ram, bharat).
male(dashrath).
male(ram).
male(bharat).
male(love).
female(kaushalya).
female(seeta).
female(urmila).
grandfather(X,Z) :- father(X,Y), father(Y,Z), male(X).
wife(X,Y) :- husband(Y,X), female(X), male(Y).
motherinlaw(X,Y) :- mother(X, Z), husband(Z, Y), female(X),
female(Y).
fatherinlaw(X,Y) :- father(X, Z), husband(Z,Y), male(X), female(Y).
Queries.
?- father(X,Y).
X = dashrath,
Y = ram ;
X = ram,
Y = love
?- mother(X,Y).
X = kaushalya,
Y = ram ;
X = seeta,
Y = love
?- motherinlaw(X,Y).
X=kaushalya,
Y=seeta
?- fatherinlaw(X,Y).
X=dashrath,
Y=seeta
?- grandfather(X,Y).
X=dashrath,
Y=love
?- sister(seeta, urmila).
Yes
?- brother(ram, shyam).
No
?- wife(X,Y).
X=kaushalya,
Y=dashrath;
X=seeta,
Y=ram
7. Basic List functions in PROLOG.
Head = ram
Tail = [shyam, mohan, sohan]
Yes
X = [a, b]
Yes
X= [a, b, d]
X=[b, c, d]
X=[d, c, b, a]
X=a
Yes
X=3
X=1
?- numlist(1, 9, X).
X=[1, 2, 3, 4, 5, 6, 7, 8, 9]
X=6
8. PROLOG program for Monkey-Banana problem.
% Constants:
% {floor, chair, bananas, monkey}
% Variables:
% {X, Y, Z}
% Predicates:
% {can-reach(X, Y) ; X can reach Y
% dexterous(X) ; X is a dexterous animal
% close(X, Y) ; X is close to Y
% get-on(X, Y) ; X can get on Y
% under(X, Y) ; X is under Y
% tall(X) ; X is tall
% in-room(X) ; X is in the room
% can-move(X, Y, Z) ; X can move Y near Z
% can-climb(X, Y)} ; X can climb onto Y
% Axioms:
in-room(bananas).
in-room(chair).
in-room(monkey).
dexterous(monkey).
tall(chair).
can-climb(monkey, chair).
can-reach(X, Y) :-
dexterous(X),
close(X, Y).
close(X, Z) :-
get-on(X, Y),
under(Y, Z),
tall(Y).
get-on(X, Y) :-
can-climb(X, Y).
under(Y, Z) :-
in-room(X),
in-room(Y),
in-room(Z),
can-move(X, Y, Z).
Queries:
?- can-reach(X, Y).
X= monkey,
Y= bananas
?- can-reach(X, bananas).
X= monkey
?- can-reach(monkey, Y).
Y=bananas
?- can-reach(monkey, bananas).
Yes
?- can-reach(lion, bananas).
No
?- can-reach(monkey, apple).
No.
9. PROLOG program for Towers of Hanoi.
move(1, X, Y, _) :-
write(X),
write(' to '),
write(Y),
nl.
move(N, X, Y, Z) :-
N>1,
M is N-1,
move(M, X, Z, Y),
move(1, X, Y,_),
move(M, Z, Y, X).
Queries:
?- move(3,left,right,center).
yes
10. PROLOG program for Best –First Search applied to the 8-puzzle
problem.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% 8-puzzle solver
%%%
%%%
%%% State have form A/B/C/D/E/F/G/H/I
%%% where {A,...,I} = {0,...,8}
%%% 0 represents the empty tile
%%%
goal(1/2/3/8/0/4/7/6/5).
s_aux(0,0) :- !.
s_aux(_,1).
s_aux(X,Y,0) :- Y is X+1, !.
s_aux(8,1,0) :- !.
s_aux(_,_,2).