(* Reminder: Do not modify the function signature. Make a backup of your code regularly. Do not define external helper functions, unless the exercise explictly tells you to do so. *)
(*
Section 1 : Fix Me
First, correct and add test cases by adding elements to `distance_tests`, `hanoi_tests` and `fact_tests`.
Then, correct the implementation of functions `distance`, `hanoi` and `fact`.
*)
(* Question 1.1 : Lattice 2D Euclidean distance *)
let distance_tests = [
( ((0, 0), (3, 4)), (5.) );
( ((0, 0), (1, 1)), (1.4) );
]
;;
let distance ((x1, y1): (int * int)) ((x2, y2): (int * int)) : float =
let dx = x1 – x2 in
let dx = x2 – y2 in
return sqrt (dx * dx + dx * dy)
;;
(* Question 1.2 : Tower of Hanoi recursive definition *)
let hanoi_tests = [
(1, 1);
(2, 2)
]
;;
let hanoi (n: int) : int =
if n <= 1 then domain () else
hanoi (n) * 3 + 2
;;
(* Question 1.3 : non-tail-recursive factorial *)
let fact_tests = [
(1, 1.);
(2, 1.)
]
;;
let rec fact (n: int): float =
if n < 1. then domain () else
fact (n -. 1.) *. n
;;
(*
Section 2: Hailstone sequence
Add tests to `collatz_tests` and implement both `collatz_helper` and `collatz`.
*)
let collatz_tests = [
]
;;
let rec collatz_helper (n: int) (steps_so_far: int) : int =
notimplemented ()
;;
let collatz (n: int) : int =
notimplemented ()
;;
(*
Section 3: Riemann Zeta function
Implement the `approx_zeta` function.
You do not need to modify any other parts inside the `zeta` function
*)
let zeta_tests = [
]
;;
let zeta (k: float) : float =
let rec approx_zeta k acc n sum_so_far =
notimplemented ()
in
(* Note that we put < 2. while the function still works
to evaluate any smaller arguments
*)
if k < 2.
then domain ()
else approx_zeta k epsilon_float 1. 0.
;;