max(P,Q,R):-P>Q,P>R,write('Larger number is '),write(P).
max(P,Q,R):-P<Q,Q>R,write('Larger number is '),write(Q).
max(P,Q,R):-R>Q,P<R,write('Larger number is '),write(R).
max(P,Q,R):-P=Q,P<R,write('Larger number is '),write(R).
max(P,Q,R):-P<Q,P=R,write('Larger number is '),write(Q).
max(P,Q,R):-Q=R,P>Q,write('Larger number is '),write(P).
max(P,Q,R):-P=Q,P>R,write('Larger numbers are '),write(P),write(' and '),write(Q).
max(P,Q,R):-P=R,Q<R,write('Larger numbers are '),write(P),write(' and '),write(R).
max(P,Q,R):-Q=R,P<R,write('Larger numbers are '),write(R),write(' and '),write(Q).
max(P,Q,R):-P=Q,P=R,write('All numbers are equal ').
Output
5 ?- max(1,2,3).
Larger number is 3
true .
6 ?- max(4,21,3).
Larger number is 21
true .
7 ?- max(4,21,34).
Larger number is 34
true .
8 ?- max(34,21,34).
Larger numbers are 34 and 34
true .
9 ?- max(34,34,34).
All numbers are equal
true
Isn't this kind of an imperative approach? It's mimicking the "if" structure.
ReplyDeleteYes it is
ReplyDelete