You are on page 1of 20

Q1. WAP in prolog to find K’th element of a List.

element_at(X,[X|_],1).
element_at(X,[_|L],K) :- K > 1,
K1 is K - 1,
element_at(X,L,K1).
Q2. WAP in prolog to reverse a list.

reverse([],Z,Z).
reverse([H|T],Z,Acc) :- reverse(T,Z,[H|Acc]).
Q3. WAP in Prolog to duplicate the elements of a list

dupli([],[]).
dupli([X|Xs],[X,X|Ys]) :- dupli(Xs,Ys).
Q4. Wap to determine prime factors of a given Positive
Integer.

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).
Q5. WAP in prolog to create a family tree.

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).
Q6. Write a predicate table /3 which prints the truth table
of a given logical expression in two variables.

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.
Q7. Implement Monkey and Banana Problem in Prolog

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).
Q8. WAP in prolog to solve the tower of Hanoi Puzzle with n=3

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
Q-9 Wap in prolog to find maximum of two numbers.

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.

Prolog can be used to perform arithmetic. Prolog supports the


common arithmetic operators:

● + addition

● - subtraction

● * multiplication

● / division

● % remainder

You might also like