Using "An Introduction To Functional Programming Through Lambda Calculus" by Greg Michaelson
Starting with
Section 4.8.3. Comparison
There are a number of ways of defining equality between numbers. One
approach is to notice that the difference between two equal numbers is
zero. However, if we subtract a number from a smaller number we also
get zero so we need to find the absolute difference between them; the
difference regardless of the order of comparison. To find the absolute
difference between two numbers, add the difference between the first
and the second to the difference between the second and the first:
def abs_diff x y = add (sub x y) (sub y x)
If they are both the same then the absolute differences will be zero
because the result of taking each from the other will be zero. If the
first is greater than the second then the absolute difference will be
the first minus the second because the second minus the first will be
zero. Similarly, if the second is greater than the first then the
difference will be the second minus the first because the first minus
the second will be zero.
Thus, we can define:
def equal x y = iszero (abs_diff x y)
We can also use subtraction to define arithmetic inequalities. For
example, a number is greater than another if subtracting the second
from the first gives a non-zero result:
def greater x y = not (iszero (sub x y))
Less is defined in the solutions to exercises section in the back.
def less x y = greater y x
Now using the book in the link just find all of the subordinate functions and you will have =, >, <. While the book does not define != it should be obvious.
EDIT
Per comment by WillNess
4.8.2. Subtraction
To find the difference between two numbers, find the difference between the numbers after decrementing both. The difference between a number and zero is the number:
rec sub x y =
if iszero y
then x
else sub (pred x) (pred y)
Please note "Now using the book in the link just find all of the subordinate functions".
I don't plan on hunting down all of the subordinate functions and listing them here because they would explode into recreating many functions here. I have read and worked through portions of the book and it is comprehensive enough that I was not lacking for info.
(<)
, which can be used as a normal function. – Sebastian Redl