Although it has no practical use, a more direct translation from the Scheme code to a corresponding Prolog code would be as follows:
% mode: sub(+, +, -)
sub(A, B, C) :-
( B = 0 % if
-> C = A % then
; A1 is A-1, % else
B1 is B-1,
sub(A1, B1, C) ).
The predicate Number is Expression
is True when Number
is the value to which Expression
evaluates.
Examples:
?- sub(12, 7, C).
C = 5.
?- sub(7, 12, C).
C = -5.
?- sub(7, 7, C).
C = 0.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…