Source

csci-350-lab1 / ack.ml

Full commit
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();;

(* The Ackermann function takes two parameters, m and n *)
let rec ack = function
   (* Ensure m and n are non-negative *)
   | m,n when m < 0 || n < 0 -> invalid_arg "Ackermann's function is only defined over the non-negative integers"
   (* If m = 0 and n is anything *)
   | 0,n -> n+1
   (* If m is anything and n = 0 *)
   | m,0 -> ack (m-1,1)
   (* Any other values for m and n *)
   | m,n -> ack (m-1,ack (m,n-1))
;;

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