Source

csci-350-lab1 / ack.ml

print_endline("Prints the Ackermann function of M and N");;
print_string("Enter M: ");;
let m = read_int();;
print_string("Enter N: ");;
let n = read_int();;

let rec ack = function
   | m,n when m < 0 || n < 0 -> invalid_arg "Ackermann's function is only defined over the non-negative integers"
   | 0,n -> n+1
   | m,0 -> ack (m-1,1)
   | m,n -> ack (m-1,ack (m,n-1))
;;

print_endline(string_of_int (ack (m,n)));;