Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

gpt 4 9 9 . .

Answered: 1 week ago

Question

2. Describe how technology can impact intercultural interaction.

Answered: 1 week ago