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
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 inequalityI'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) .
great(X,Y,Z) :- ...
as meaningX
is the greater ofY
andZ
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 isY
is the greater ofY
andZ
if... andgreat(Z, Y, Z).
which isZ
is the greater ofY
andZ
if.... - lurker