You are on page 1of 15

INDEX

Sr. No. Program Date Sign


1. Wap in prolog to find K’th element 13-01-2021
of a List

2. Wap in prolog to reverse a list 19-01-2021


3. Prolog to duplicate the elements 25-01-2021
of a list

4. Determine prime factors of a 28-01-2021


given Positive Integer

5. Prolog to create a family tree. 01-02-2021


6. Predicate table /3 which prints the 08-02-2021
truth table

7. Implement Monkey and Banana 15-02-2021


Problem

8. Prolog to solve the tower of Hanoi 18-02-2021


Puzzle with n=3.

9. Prolog to find maximum of two 22-02-2021


numbers

10. Explain various arithmetic 02-03-2021


Operators in Prolog
Q1. Wap in prolog to find K’th element of a List.
Ans: -
Code snippet

element_at(X,[X|_],1).
element_at(X,[_|L],K) :- K > 1,
K1 is K - 1,
element_at(X,L,K1).

Input :- element_at(X,[1,5,7,8,9,11],4).
Q2. Wap in prolog to reverse a list.
Ans:-
Code snippet

reverse([],Z,Z).
reverse([H|T],Z,Acc) :- reverse(T,Z,[H|Acc]).

Input- reverse([5,9,11,20],X,[]).
Q3. WAP in Prolog to duplicate the elements of a list
Ans:-
Code snippet
dupli([],[]).
dupli([X|Xs],[X,X|Ys]) :- dupli(Xs,Ys).

Input - dupli([1,2,5,7],X).
Q4. Wap to determine prime factors of a given Positive Integer.
Ans:-
Code snippet

prime_factors(N,L) :- N > 0, prime_factors(N,L,2).


prime_factors(1,[],_) :- !.
prime_factors(N,[F|L],F) :-
R is N // F, N =:= R * F, !, prime_factors(R,L,F).
prime_factors(N,L,F) :-
next_factor(N,F,NF), prime_factors(N,L,NF).
next_factor(_,2,3) :- !.
next_factor(N,F,NF) :- F * F < N, !, NF is F + 2.
next_factor(N,_,N).

Input- prime_factors(1250, L).


Q5. WAP in prolog to create a family tree.
Ans:-

Code snippet
male(shankar).
male(ulhas).
male(satish).
male(saurabh).
male(prashant).

female(umabai).
female(mrunal).
female(sadhana).
female(swati).

parent(shankar,umabai,ulhas).
parent(shankar,umabai,satish).
parent(ulhas,mrunal,prashant).
parent(satish,sadhana,saurabh).
parent(satish,sadhana,swati).

brother(ulhas,satish).
brother(satish,ulhas).
brother(prashant,saurabh).
brother(saurabh,prashant).
sister(swati,saurabh).
sister(swati,prashant).
father(X,Y) :- parent(X,Z,Y).
mother(X,Y) :- parent(Z,X,Y).
son(X,Y,Z) :- male(X),father(Y,X),mother(Z,X).
daughter(X,Y,Z) :- female(X),father(Y,X),mother(Z,X).
wife(X,Y) :- female(X),parent(Y,X,Z).
grandfather(X,Y) :- male(X),father(X,Z),father(Z,Y).
uncle(X,Y) :- male(X),brother(X,Z),father(Z,Y).
aunt(X,Y) :- wife(X,Z),uncle(Z,Y).
cousin(X,Y) :- father(Z,X),brother(Z,W),father(W,Y).
ancestor(X,Y,Z) :- parent(X,Y,Z).
ancestor(X,Y,Z) :- parent(X,Y,W),ancestor(W,U,Z).
Input- parent(X,Y,Z).
Q6. Write a predicate table /3 which prints the truth table of a given
logical expression in two variables
Ans:-
Code Snippet

and(A,B) :- A, B.
or(A,_) :- A.
or(_,B) :- B.
equ(A,B) :- or(and(A,B), and(not(A),not(B))).
xor(A,B) :- not(equ(A,B)).
nor(A,B) :- not(or(A,B)).
nand(A,B) :- not(and(A,B)).
impl(A,B) :- or(not(A),B).
bind(true).
bind(false).
table(A,B,Expr) :- bind(A), bind(B), do(A,B,Expr), false.
do(A,B,_) :- write(A), write(' '), write(B), write(' '), false.
do(_,_,Expr) :- Expr, !, write(true), nl.
do(_,_,_) :- write(false), nl.
Input- table(A,B,and(A,or(A,B))).

Q7. Implement Monkey and Banana Problem in Prolog


Ans:-
Code snippet
move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(state(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
drag(P1,P2),
state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(State1) :-
move(State1,_,State2),
canget(State2).
Input- canget(state(atdoor, onfloor, atwindow, hasnot)).
Q8. WAP in prolog to solve the tower of Hanoi Puzzle with n=3
Ans-
move(3, source, target, auxiliary).
 Move top disk from source to target
 Move top disk from source to auxiliary
 Move top disk from target to auxiliary
 Move top disk from source to target
 Move top disk from auxiliary to source
 Move top disk from auxiliary to target
 Move top disk from source to target

Code Snippet:-

move(1,X,Y,_) :-
write('Move top disk from '), 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).

Output:

| ?- [towersofhanoi].
compiling D:/TP Prolog/Sample_Codes/towersofhanoi.pl for byte
code...
D:/TP Prolog/Sample_Codes/towersofhanoi.pl compiled, 8 lines read
- 1409 bytes written, 15 ms

yes
| ?- move(4,source,target,auxiliary).
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
Move top disk from source to auxiliary
Move top disk from target to source
Move top disk from target to auxiliary
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
Move top disk from auxiliary to source
Move top disk from target to source
Move top disk from auxiliary to target
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target

true ?

(31 ms) yes

Q-9 Wap in prolog to find maximum of two numbers.


Ans-

max(X,Y):-
(
X=Y ->
write('both are equal')
;
X>Y ->
(
Z is X,
write(Z)
)
;
(
Z is Y,
write(Z)
)
).
Q-10 Wap to explain various arithmetic Operators in Prolog.
Ans-

Prolog can be used to perform arithmetic. Prolog supports the common arithmetic
operators:

 + addition
 - subtraction
 * multiplication
 / division
 % remainder
?- X is 45345.
X = 45345

yes

?- X is -876878.
X = -876878

yes

?- X is 2+1.
X=3

yes

?- X is 12-5.
X=7

yes

?- X is 6*7.
X = 42

yes

?- X is 6//2.
X=3

yes

?- X is 7/2.
X = 3.5

yes

?- X is 7 * 4 // 2.
X = 14

yes

?- X is 5 mod 3.
X=2

yes

?- X is 2147483647 + 1.
X = 2147483648

yes

?- X is -2147483648 - 1.
X = -2147483649

yes

You might also like