I am taking a class on programming paradigms. Currently we are learning Prolog, and I am stuck in a different paradigm. One of the functions I'm trying to convert from imperative is a relatively simple function.
foo(A,B,C)
if(A > B) C is 1
if(A < B) C is -1
if(A = B) C is 0
I could do this in Prolog pretty easily.
foo(A,B,C) :- sub(A-B,C).
sub(E, C) :- E = 0, C is 0.
sub(E, C) :- E > 0, C is 1.
sub(E, C) :- E < 0, C is -1.
The problem is, I can only use one "is" in the entire predicate (can't define a smaller predicate that calls Is and calls that instead or anything), and I cannot use Prolog's if/else constructs. I cannot figure out how to think about this problem in a declarative manner.
I thought maybe I could do something like C is (A-B)/abs(A-B), but that breaks on A=B, and would require 2 "is" statements. I am just stuck.