1
votes

Comparing 2 numbers: define a predicate great that takes 3 parameters - the 2 numbers that I want to compare them and the output variable that return the greatest number.

Ex.

?- great(X,3,5). 
X=5. 

in Prolog language

1
Do you mean Prolog? Or is there some kind of "bro"-variant of Prolog named "brolog"? :-D - Sergii Dymchenko
prolog sorry its my fault - user3527224
@Sergey that's funny. :D - lurker
@user3527224 have you looked at any documentation at all? Go find a Prolog manual online for GNU Prolog or SWI Prolog and look at the comparison operators. Then think of your predicate great(X,Y,Z) :- ... as meaning X is the greater of Y and Z if.... You can have more than one clause for the predicate. So to make it simple, you could have two: great(Y, Y, Z) :-... which is Y is the greater of Y and Z if... and great(Z, Y, Z). which is Z is the greater of Y and Z if.... - lurker

1 Answers

1
votes

You really should read the documentation. Maybe even read a good book on the language:

  • </2 is "less than"
  • >=/2 is "greater than or equal to"
  • =</2 is "less than or equal to"

For "equal to", you could use:

  • =/2 is (roughly) "equal to" (unifiable with)
  • =:=/2 is arithmetic equality
  • =\=/2 is arithmetic inequality

I'm sure you can figure things out.

Alternatively, you could avoid Prolog's comparison operators entirely and simply say

gt(X,Y,Z) :- Z is max(X,Y) .

or use a discriminant function to select the desired value:

gt(X,Y,Z) :- D is sign(X-Y) + 1 , nth0(D,[Y,X,X],Z) .