Professional Documents
Culture Documents
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
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))).
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 ?
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