Question

This is a programming question of Ocaml language (* 5. Consider the following higher-order function reduce...

This is a programming question of Ocaml language

(* 5. Consider the following higher-order function reduce *)

let rec reduce (f:'a -> 'b -> 'b) (u:'b) (xs:'a list) : 'b =
match xs with
| [] -> u
| hd::tl -> f hd (reduce f u tl);;

(* Consider the following functions defined using reduce *)

let sum xs = reduce (fun x y -> x+y) 0 xs
let prod xs = reduce (fun x y -> x*y) 1 xs

(* What do the sum and prod functions do? What does the reduce
function do? Trace the following code to help figure this out.
Show your trace and provide your explanation here:

Homework Answers

Answer #1

sum takes an integer list as an input and outputs the sum of the elements

prod takes an integer list as an input and outputs the product of the elements

let rec reduce (f:'a -> 'b -> 'b) (u:'b) (xs:'a list) : 'b -> Input to reduce is a function, initial value of the answer, and a list of integer and it outputs an integer

match xs with -> Then it checks if list is empty or not

| [] -> u -> If list is empty return the initial value which came as input

| hd::tl -> f hd (reduce f u tl);; -> If list is not empty apply the function to first element of the list and the ouput from recursive call to the same function.

Example: sum [1;2;3]

reduce(f,0,[1;2;3]) -> 1 + reduce(f,0,[2;3]) -> 2 + reduce(f,0,[3]) -> 3 + reduce(f,0,[])

reduce(f,0,[]) returns u;

Similar recusive call is made for prod

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x)...
Using R programming language, complete the following. 1. Generate the bivariate normal sample of size 100...
Using R programming language, complete the following. 1. Generate the bivariate normal sample of size 100 with parameters a. marginal mean of X equal to 1, b. marginal mean of Y equal to 2, c. marginal variance of X equal to 3, d. marginal variance of Y equal to 4, e. correlation between X and Y equal to -1/2. You can use the function mvrnorm(), first installing its package by the code if (! require ("MASS")) install.packages("MASS"); library ("MASS") 2....
Question 5 options: Consider the following integer linear programming problem: Max Z =       3x +...
Question 5 options: Consider the following integer linear programming problem: Max Z =       3x + 2y Subject to:    3x + 5y ? 30 4x + 2y ? 28                     x ? 8                     x , y ? 0 and integer The solution to the linear programming formulation is: x = 5.714, y = 2.571. What is the optimal solution to the integer linear programming problem? State the optimal values of decision variables and the value of the objective function.
How would I write the following program in C programming language? Function header: int sumsort(int *a,...
How would I write the following program in C programming language? Function header: int sumsort(int *a, int *b, int *c) This function should arrange the 3 values in the memory locations pointed to by a, b, and c in ascending order and also return the sum of the contents of the memory locations a, b, and c.
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Question 5 options: Consider the following integer linear programming problem: Max Z =       3x +...
Question 5 options: Consider the following integer linear programming problem: Max Z =       3x + 2y Subject to:    3x + 5y ≤ 30 5x + 2y ≤ 28                     x ≤ 8                     x, y ≥ 0 and integer The solution to the linear programming formulation is: x = 4.21, y = 3.47. What is the optimal solution to the integer linear programming problem? State the optimal values of decision variables. x = , y =
Show the following: a) Let there be Y with the cumulative distribution function F(y). Let F(Y)=Z....
Show the following: a) Let there be Y with the cumulative distribution function F(y). Let F(Y)=Z. Show that Z~U(0,1) for F(y). b) Let X~U(0,1), and let Y := -ln(X). Show that Y~exp(1)
Consider the following utility functions over goods X and Y for two individuals: Daves utility function...
Consider the following utility functions over goods X and Y for two individuals: Daves utility function : Ud(x,y) = 6min(x,y) Jess's Utility function is Uj (x,y) = 6x + 3Y Draw Daves indifference curve map for two specific utility levels: U= 6 and U = 12 Draw Jess indifference curve map for two seperate utility levels: U=6 and U=12 Explain the preferences between the two What is the MRS for Dave? and for Jess?
Consider the following linear programming problem: Max 8X + 7Y s.t. 15X + 5Y ≤ 75...
Consider the following linear programming problem: Max 8X + 7Y s.t. 15X + 5Y ≤ 75 10X + 6Y ≤ 60 X + Y ≤ 8 X, Y ≥ 0 The optimal value of the objective function is ________. A)59 B)61 C)58 D)60
Consider a consumer with the following utility function: U(X, Y ) = XY. (a) Derive this...
Consider a consumer with the following utility function: U(X, Y ) = XY. (a) Derive this consumer’s marginal rate of substitution, MUX/MUY (b) Derive this consumer’s demand functions X∗ and Y∗. (c) Suppose that the market for good X is composed of 3000 identical consumers, each with income of $100. Derive the market demand function for good X. Denote the market quantity demanded as QX. (d) Use calculus to show that the market demand function satisfies the law-of-demand.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT