Prolog - a predicate to compare three arguments

902 Views Asked by At

I am new into prolog and I've already done some coding, but I have a task I cannot handle. Could you please tell me how to describe a working predicate, that compares arg1, arg2 and arg3 and returns yes it arg1>arg2>arg3? Many thanks in advance!

Szymon

1

There are 1 best solutions below

2
damianodamiano On

The solution is quite easy:

compare3(X, Y, Z):-
    X>Y,
    Y>Z.

?- compare3(5,4,3).
true.

Keep in mind that you cannot define predicates with an arbitrary number of input parameters (so obviously compare/3 could be called only with 3 input). To make it more flexible you can insert the element in a list and rewrite it like this

myCompare([]).
myCompare([_]):- !.
myCompare([A,B|T]):-
    A>B,
    myCompare([B|T]).

?- myCompare([5,4,3]).
true.

Now myCompare/1 accepts a list and return true if the list is sorted, false otherwise.