-4

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

grzemski
  • 1
  • 2
  • 1
    By "returns yes" I assume you mean that it succeeds (which is not, technically "returning a value"). Start with `compare3(X, Y, Z) :- ...` and determine what `...` is based upon the statement: `*`X` > `Y` > `Z` **if** ...*. To say any more than that would be doing all of your work for your. :p – lurker Jan 22 '18 at 11:57

1 Answers1

2

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.

damianodamiano
  • 2,528
  • 2
  • 13
  • 21