Source

csci-350-lab1 / fib.ml

Full commit
print_endline("Prints the Nth Fibonacci Number");;
print_string("Enter N: ");;

let limit = read_int();;

(* The Fibonacci sequence is generated by the sum of the previous two numbers *)
let rec fib = function
   (* If n < 0 throw an error *)
   | n when n < 0 -> invalid_arg "N must be a positive integer"
   (* The 0th term is defined as 0 *)
   | 0 -> 0
   (* The 1st term is defined as 1 *)
   | 1 -> 1
   (* Recurse to find previous values *)
   | n -> fib(n - 1) + fib(n - 2)
;;

print_endline(string_of_int (fib limit));;