Question: 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:

  1. We want to always start our index (n) from one, and we allow only positive integers (raise (Failure "Argument Not Positive").
  2. We allow the caller to define what f describes the sum.
  3. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!