%% $Id: ackermann.mercury,v 1.3 2001/05/13 01:22:35 doug Exp $
%% http://www.bagley.org/~doug/shootout/
:- module mytest.
:- interface.
:- import_module io.
:- pred main(io__state, io__state).
:- mode main(di, uo) is det.
:- implementation.
:- func ack(int, int) = int.
:- mode ack(in, in) = out is det.
:- import_module string, list, int.
:- pred mytest(int, io__state, io__state).
:- mode mytest(in, di, uo) is det.
ack(M, N) = R :-
( if M = 0 then
R = N + 1
else if N = 0 then
R = ack(M - 1, 1)
else
R = ack(M - 1, ack(M, N - 1))
).
mytest(Num) -->
io__write_string("Ack(3,"),
io__write_int(Num),
io__write_string("): "),
io__write_int(ack(3,Num)),
io__write_string("\n").
main -->
io__command_line_arguments(Args),
( if { Args = [] } then
mytest(1)
else if { Args = [Arg|_] } then
( if { string__to_int(Arg, N), N > 0 } then
mytest(N)
else
mytest(1)
)
).