-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.pl
More file actions
30 lines (25 loc) · 792 Bytes
/
Copy pathdb.pl
File metadata and controls
30 lines (25 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%% given an integer it returns an ordered list containing
%% all the precendent elements up to the integer given
make_list(X, 0) :- X = [0].
make_list(List, Value) :-
Next is Value - 1, Next >= 0,
make_list(NextList, Next),
append(NextList, [Value], List).
%% multiply all elemnts in the given list
mult_list([H|[]], H).
mult_list([H|T], R) :-
mult_list(T, P),
R is H * P.
%% calculate the factorial of the given value
%% using a list internally
factorial(0, 0).
factorial(Fact, Value):-
make_list([_|Tail], Value),
mult_list(Tail, Fact).
%% calculate the factorial of the given value
%% without using a list internally
factorial_no_list(1, 1).
factorial_no_list(Fact, Value) :-
Next is Value - 1, Value > 0,
factorial_no_list(NextFact, Next),
Fact is Value * NextFact.