%% $Id: fibo.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 fib(int) = int.
:- mode fib(in) = out is det.

:- import_module string, list, int.

:- pred mytest(int, io__state, io__state).
:- mode mytest(in, di, uo) is det.

fib(N) = R :- 
    ( if N < 2 then
    R = 1
      else
    R = fib(N-2) + fib(N-1)
    ).

mytest(Num) -->
    io__write_int(fib(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)
         )
    ).