-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch30.pl
72 lines (61 loc) · 1.67 KB
/
ch30.pl
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
:- use_module(library(clpfd)).
horror_factorial(0, 1) :- !.
horror_factorial(N, F) :-
N > 0,
N1 is N - 1,
horror_factorial(N1, F1),
F is N*F1.
% ?- horror_factorial(N, F).
% N = 0,
% F = 1.
% ?- horror_factorial(3, F).
% F = 6.
% ?- horror_factorial(N, 8).
% ERROR: Arguments are not sufficiently instantiated
% ERROR: In:
% ERROR: [13] _21412>0
% ERROR: [12] horror_factorial(_21438,8) at /Users/mike/Kata/the-power-of-prolog/ch30.pl:5
% ERROR: [11] toplevel_call(user:user: ...) at /usr/local/Cellar/swi-prolog/9.2.6/lib/swipl/boot/toplevel.pl:1317
horror_factorial1(0, 1).
horror_factorial1(N, F) :-
N > 0,
N1 is N - 1,
horror_factorial1(N1, F1),
F is N*F1.
% ?- horror_factorial1(N, F).
% N = 0,
% F = 1 ;
% ERROR: Arguments are not sufficiently instantiated
% ERROR: In:
% ERROR: [13] _68840>0
% ERROR: [12] horror_factorial1(_68866,_68868) at /Users/mike/Kata/the-power-of-prolog/ch30.pl:26
% ERROR: [11] toplevel_call(user:user: ...) at /usr/local/Cellar/swi-prolog/9.2.6/lib/swipl/boot/toplevel.pl:1317
% ^ Exception: (4) setup_call_cleanup('$toplevel':notrace(call_repl_loop_hook(begin, 0)), '$toplevel':'$query_loop'(0), '$toplevel':notrace(call_repl_loop_hook(end, 0))) ? abort
% % Execution Aborted
n_factorial(0, 1).
n_factorial(N, F) :-
N #> 0,
N1 #= N - 1,
n_factorial(N1, F1),
F #= N * F1.
% ?- n_factorial(N, F).
% N = 0,
% F = 1 ;
% N = F, F = 1 ;
% N = F, F = 2 ;
% N = 3,
% F = 6 ;
% N = 4,
% F = 24 ;
% N = 5,
% F = 120 . % and so on...
% ?- n_factorial(3, F).
% F = 6 ;
% false.
% ?- n_factorial(N, 8).
% ^CAction (h for help) ? abort
% % Execution Aborted
% ?- n_factorial(N, 24).
% N = 4 ;
% ^CAction (h for help) ? abort
% % Execution Aborted