Skip to content

Instantly share code, notes, and snippets.

@MrMikeFloyd
Created December 26, 2019 11:00
Show Gist options
  • Save MrMikeFloyd/4be5e9f94bc87536cf7fd2c76453a531 to your computer and use it in GitHub Desktop.
Save MrMikeFloyd/4be5e9f94bc87536cf7fd2c76453a531 to your computer and use it in GitHub Desktop.
Implementation for the infamous Ackermann Function in SWI-Prolog
%
% Ackermann function
% ackermann(X,Y,R)
%Base cases
ackermann(0,0,R) :- R is 1.
ackermann(0,Y,R) :- Y>0, !, R is Y+1. %Stop if Y=0
%Cases leading to recursion
ackermann(X,0,R) :-
X>0, !, %Cut, if X=0
TmpX is X-1,
ackermann(TmpX, 1, R).
ackermann(X,Y,R) :-
X>0,
Y>0, %Only continue if X>0 && Y>0
!,
TmpX is X-1,
TmpY is Y-1,
ackermann(X,TmpY,TmpR),
ackermann(TmpX,TmpR,R).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment