Question
On the LearnOCaml system we have installed incorrect code for the following functions: val double : int -> int = val fact : int ->
On the LearnOCaml system we have installed incorrect code for the following functions:
val double : int -> int =val fact : int -> float =
The function double is supposed to take a non-negative integer n 0 and return its value doubled. Its type is as shown above. The function fact is supposed to take a non-negative integer n 0 and return n! as a floating-point number. We did this to give you some experience with changing types; there is no good mathematical reason for doing this1. The implementations we have provided may have logical, mathematical or syntactic errors. Please fix them.
(* Q1 TODO: Correct these tests for the double function. *) let double_tests = [ (0, 1); (1, 1); (3, 5); ]
(* Q1 TODO: Correct this implementation so that it compiles and returns the correct answers. *) let double int = match n with | n -> 2 + double n - 1 | 0 -> 0
(* Q1 TODO: Write your own tests for the fact function. See the provided tests for double, above, for how to write test cases. Remember that you should NOT test cases for n < 0. *) let fact_tests = [ (* Your test cases go here. Remember that the outputs of fact should be *floating-point* numbers. *) ]
(* Q1 TODO: Correct this implementation so that it compiles and returns the correct answers. *) let rec fact (n: int): float = match n with | 0 -> 1.0 | _ -> n * factorial n - 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started