+1 (315) 557-6473 

Calculate Value Of Function Using Recursion In Ocaml Assignment Solution.


Instructions

Objective
Write a program to calculate the value of function using recursion in Ocaml.

Requirements and Specifications

Ocaml- Test the code on Dune system to get the "result", On Mac IOS.
Calculate value of function using recursion in Ocaml
Calculate value of function using recursion in Ocaml 1
Screenshots of output
Calculate value of function using recursion in Ocaml 2
Source Code
open Printf
(*************************************************
0) Fill in your name and PID on the lines below.
*)
let name = "YOUR NAME";;
let id = "YOUR PID OR ID";;
let _ = printf "\n-----NAME: %s ID: %s -----\n" name id;;
let todo (type t) (x : t) : 'a =
  let module M = struct exception Todo of t end in
  raise @@ M.Todo x
;;
open Num
(*
Suppose
  f(0) = 1,
  f(1) = 2,
  6*f(n)*f(n+1) - 5*f(n)*f(n+2) + f(n+1)*f(n+2) = 0
Calculate f(n) for any integer n >= 0.
*)
let rec f(n:int): string = (* Note that the result type is string. Whatever big
integer you use, convert it back to string in the end. *)
  let f_n (fn_2:num) (fn_1:num): num =
    div_num (mult_num (mult_num (num_of_int 6) fn_2) fn_1)
            (sub_num (mult_num (num_of_int 5) fn_2) fn_1)
  in
  let rec f_p (m:int) (fn_2:num) (fn_1:num): num =
    if m = n then
      fn_1
    else
      f_p (m + 1) fn_1 (f_n fn_2 fn_1)
  in
  if n = 0 then
    string_of_int 1
  else if n = 1 then
    string_of_int 2
  else
    let f0 = num_of_string (f 0) in
    let f1 = num_of_string (f 1) in
    string_of_num (f_p 1 f0 f1)
;;
printf "%d - %s\n" 7 (f 7); (* This is your own way of quick manual testing. *)
open OUnit
module Tests = struct
  let testingPrint act exp =
    print_string "Expected ";
    print_string exp;
    print_endline "\n got ";
    print_string act;
    print_endline "\n-----";;
  let testing n expectedOutput =
    let act = f n in
      printf "Testing func %d:\n" n;
      testingPrint act expectedOutput;
      print_endline "";
      assert_equal expectedOutput act;;
  let suite =
    "func" >:::
        [
          "n = 14" >:: (fun _ -> testing 14 "16384";);
          "n = 17" >:: (fun _ -> testing 17 "131072";);
          "n = 21" >:: (fun _ -> testing 21 "2097152";);
          "n = 35" >:: (fun _ -> testing 35 "34359738368";);
          "n = 43" >:: (fun _ -> testing 43 "8796093022208";);
          "n = 55" >:: (fun _ -> testing 55 "36028797018963968";);
          "n = 67" >:: (fun _ -> testing 67 "147573952589676412928";);
          "n = 71" >:: (fun _ -> testing 71 "2361183241434822606848";);
          "n = 81" >:: (fun _ -> testing 81 "2417851639229258349412352";);
        ]
;;
end
let _ =
  run_test_tt_main Tests.suite
;;