Answered step by step
Verified Expert Solution
Question
1 Approved Answer
OCAML psum (fun x -> x * x ) equates to the following function of n. f (n) = 1 * 1 + 2 *
OCAML
psum (fun x -> x * x ) equates to the following function of n.
f (n) = 1 * 1 + 2 * 2 + ... + n * n
Rules:
- We want to always start our index (n) from one, and we allow only positive integers (raise (Failure "Argument Not Positive").
- We allow the caller to define what f describes the sum.
- We want to return a function (specifically a function awaiting the value of n)
Click for Sample Unit Test
let f x = 3 * x * x + 5 * x + 9 in (** f (x) = 3x^2 + 5x + 9 **) let partial_sum = psum f in if partial_sum 1 = 17 && partial_sum 5 = 285 then print_string "YAY" else raise (Failure "OOPS");
NB: Notice that the type of this function is psum: (int -> int) -> (int -> int) which is to say a function which takes one arguments a function (which takes a int to int) and returns another function (int to int
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