Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Ackermann-function/Prolog/ackermann-function-1.pro
Normal file
5
Task/Ackermann-function/Prolog/ackermann-function-1.pro
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
:- table ack/3. % memoization reduces the execution time of ack(4,1,X) from several
|
||||
% minutes to about one second on a typical desktop computer.
|
||||
ack(0, N, Ans) :- Ans is N+1.
|
||||
ack(M, 0, Ans) :- M>0, X is M-1, ack(X, 1, Ans).
|
||||
ack(M, N, Ans) :- M>0, N>0, X is M-1, Y is N-1, ack(M, Y, Ans2), ack(X, Ans2, Ans).
|
||||
16
Task/Ackermann-function/Prolog/ackermann-function-2.pro
Normal file
16
Task/Ackermann-function/Prolog/ackermann-function-2.pro
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
ack(0,N,s(N)).
|
||||
ack(s(M),0,P):- ack(M,s(0),P).
|
||||
ack(s(M),s(N),P):- ack(s(M),N,S), ack(M,S,P).
|
||||
|
||||
% Peano's first axiom in Prolog is that s(0) AND s(s(N)):- s(N)
|
||||
% Thanks to this we don't need explicit N > 0 checks.
|
||||
% Nor explicit arithmetic operations like X is M-1.
|
||||
% Recursion and unification naturally decrement s(N) to N.
|
||||
% But: Prolog clauses are relations and cannot be replaced by their result, like functions.
|
||||
% Because of this we do need an extra argument to hold the output of the function.
|
||||
% And we also need an additional call to the function in the last clause.
|
||||
|
||||
% Example input/output:
|
||||
% ?- ack(s(0),s(s(0)),P).
|
||||
% P = s(s(s(s(0)))) ;
|
||||
% false.
|
||||
Loading…
Add table
Add a link
Reference in a new issue