%{
ats_int_type
fact_in_c (ats_int_type n) {
int res = 1;
while (n > 0) res *= n-- ;
return res ;
}
%}
extern fun fact {n:nat} (n: int n): Nat = "fact_in_c"
fn fact_usage (cmd: string): void =
prerrf ("Usage: %s [integer]\n", @(cmd))
implement main (argc, argv) = begin
if argc >= 2 then let
val n = int1_of argv.[1] val () = assert_errmsg
(n >= 0, "The integer argument needs to be nonnegative.\n")
val res = fact n
in
printf ("factorial of %i = %i\n", @(n, res))
end else begin
fact_usage (argv.[0]); exit (1)
end
end