Prolog predicates can be given the role of arithmetic function. The
last argument is used to return the result, the arguments before the
last are the inputs. Arithmetic functions are added using the predicate arithmetic_function/1,
which takes the head as its argument. Arithmetic functions are module
sensitive, that is they are only visible from the module in which the
function is defined and declared. Global arithmetic functions should be
defined and registered from module user
. Global definitions
can be overruled locally in modules. The built-in functions described
above can be redefined as well.
user
. Example:
1 ?- [user]. :- arithmetic_function(mean/2). mean(A, B, C) :- C is (A+B)/2. user compiled, 0.07 sec, 440 bytes. Yes 2 ?- A is mean(4, 5). A = 4.500000