Tricky Version
From LiteratePrograms
% Tricky Version
:- dynamic acr/3.
try_asserta(F) :-
F, !.
try_asserta(F) :-
!, asserta(F).
ackermann(M, N, A) :-
acr(M, N, A), !.
ackermann(0, N, A) :-
!, A is N + 1, try_asserta(acr(0, N, A)).
ackermann(M, 0, A) :-
%M > 0, !, M1 is M - 1, ackermann(M1, 1, A), try_asserta(acr(M, 0, A)), try_asserta(acr(M1, 1, A)).
ackermann(M, N, A) :-
%M > 0, %N > 0, !, M1 is M - 1, N1 is N - 1, ackermann(M, N1, Temp), try_asserta(acr(M, N1, Temp)), ackermann(M1, Temp, A), try_asserta(acr(M, N, A)), try_asserta(acr(M1, Temp, A)).
Download code |