This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,2 @@
fact(X, 1) :- X<2.
fact(X, F) :- Y is X-1, fact(Y,Z), F is Z*X.

View file

@ -0,0 +1,8 @@
fact(N, NF) :-
fact(1, N, 1, NF).
fact(X, X, F, F) :- !.
fact(X, N, FX, F) :-
FX1 is FX * X,
X1 is X + 1,
fact(X1, N, FX1, F).

View file

@ -0,0 +1,13 @@
% foldl(Pred, Init, List, R).
%
foldl(_Pred, Val, [], Val).
foldl(Pred, Val, [H | T], Res) :-
call(Pred, Val, H, Val1),
foldl(Pred, Val1, T, Res).
% factorial
p(X, Y, Z) :- Z is X * Y).
fact(X, F) :-
numlist(2, X, L),
foldl(p, 1, L, F).

View file

@ -0,0 +1,12 @@
:- use_module(lambda).
% foldl(Pred, Init, List, R).
%
foldl(_Pred, Val, [], Val).
foldl(Pred, Val, [H | T], Res) :-
call(Pred, Val, H, Val1),
foldl(Pred, Val1, T, Res).
fact(N, F) :-
numlist(2, N, L),
foldl(\X^Y^Z^(Z is X * Y), 1, L, F).

View file

@ -0,0 +1,14 @@
:- use_module(lambda).
fact(N, FN) :-
cont_fact(N, FN, \X^Y^(Y = X)).
cont_fact(N, F, Pred) :-
( N = 0 ->
call(Pred, 1, F)
; N1 is N - 1,
P = \Z^T^(T is Z * N),
cont_fact(N1, FT, P),
call(Pred, FT, F)
).