Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,2 @@
:- arithmetic_function((^^)/2).
:- op(200, xfy, user:(^^)).

View file

@ -0,0 +1,24 @@
%% ^^/3
%
% True if Power is Base ^ Exp.
^^(Base, Exp, Power) :-
( Exp < 0 -> Power is 1 / (Base ^^ (Exp * -1)) % If exponent is negative, then ...
; Exp > 0 -> length(Powers, Exp), % If exponent is positive, then
foldl( exp_folder(Base), Powers, 1, Power ) % Powers is a list of free variables with length Exp
% and Power is Powers folded with exp_folder/4
; Power = 1 % otherwise Exp must be 0, so
).
%% exp_folder/4
%
% True when Power is the product of Base and Powers.
%
% This predicate is designed to work with foldl and a list of free variables.
% It passes the result of each evaluation to the next application through its
% fourth argument, instantiating the elements of Powers to each successive Power of the Base.
exp_folder(Base, Power, Powers, Power) :-
Power is Base * Powers.

View file

@ -0,0 +1,11 @@
?- X is 2 ^^ 3.
X = 8.
?- X is 2 ^^ -3.
X = 0.125.
?- X is 2.5 ^^ -3.
X = 0.064.
?- X is 2.5 ^^ 3.
X = 15.625.

View file

@ -0,0 +1,16 @@
exp_recursive(Base, NegExp, NegPower) :-
NegExp < 0,
Exp is NegExp * -1,
exp_recursive_(Base, Exp, Base, Power),
NegPower is 1 / Power.
exp_recursive(Base, Exp, Power) :-
Exp > 0,
exp_recursive_(Base, Exp, Base, Power).
exp_recursive(_, 0, 1).
exp_recursive_(_, 1, Power, Power).
exp_recursive_(Base, Exp, Acc, Power) :-
Exp > 1,
NewAcc is Base * Acc,
NewExp is Exp - 1,
exp_recursive_(Base, NewExp, NewAcc, Power).