% ---------------------------------------------------------------------------- %
% nestedloop.m
% Ralph Becket <rbeck@microsoft.com>
% Tue Jan  9 13:36:26 GMT 2001
% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix
%
% ---------------------------------------------------------------------------- %

:- module mytest.

:- interface.

:- import_module io.



:- pred main(io__state, io__state).
:- mode main(di, uo) is det.



:- implementation.

:- import_module list, int, std_util, string, require.



main -->
    io__command_line_arguments(ArgV),
    (   { ArgV = [],        N = 1 }
    ;   { ArgV = [Arg],     N = string__det_to_int(Arg) }
    ;   { ArgV = [_,_|_],   error("usage: nestedloop [N]") }
    ),
    io__write_int(nested_loop(N)),
    io__nl.



:- func nested_loop(int) = int.

nested_loop(N) =
    loop(N, loop(N, loop(N, loop(N, loop(N, loop(N, plus(1)))))), 0).



:- func loop(int, func(int) = int, int) = int.

loop(I, Fn, X) = ( if I > 0 then loop(I - 1, Fn, Fn(X)) else X ).