/*
Ackermann function in Picat.
From Rosetta code:
http://rosettacode.org/wiki/Ackermann_function
"""
The Ackermann function is a classic recursive example in computer science.
It is a function that grows very quickly (in its value and in the size of
its call tree). It is defined as follows:
A(m, n) = n+1 if m = 0
A(m-1, 1) if m > 0 and n = 0
A(m-1, A(m, n-1)) if m > 0 and n > 0
Its arguments are never negative and it always terminates. Write a function
which returns the value of A(m,n). Arbitrary precision is preferred (since
the function grows so quickly), but not required.
"""
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => go.
go =>
Ack = a(3,4),
writeln(ack=Ack),
foreach(I in 0..3, J in 0..6)
printf("a(%d,%d): %d\n", I,J, a(I,J))
end,
foreach(J in 0..1)
printf("a(4,%d): %d\n", J, a(4,J))
end,
nl.
table
a(M, N) = N+1, M==0 => true.
a(M, N) = a(M-1,1), (M > 0, N==0) => true.
a(M, N) = a(M-1,a(M, N-1)), (M > 0, N > 0) => true.