Question

You're given the type length = In of float | Cm of float. Write an OCaml...

You're given the type length = In of float | Cm of float. Write an OCaml function addlen(x,y) that adds two lengths. If x is in inches, the sum should be in inches; if x is in centimeters, the sum should be in centimeters.

Homework Answers

Answer #1

Here is the complete OCaml Code for the addlen function. I have added comments for a better understanding of the same:

let addlen(x,y) = match (x,y) with

| (In(a), In(b)) -> In(a +. b) (* Answer is in Inches *)

| (In(a), Cm(b)) -> In(a +. (b /. 2.54)) (* Answer is in Inches *)

| (Cm(a), Cm(b)) -> Cm(a +. b) (* Answer is in cms *)

| (Cm(a), In(b)) -> Cm(a +. (b *. 2.54));; (* Answer is in cms *)

You can comment below the answer in case of any doubts and I will be happy to help.

Please give a thumbs up if the answer could be of help!

All the best!

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
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
Given the following function prototypes: float asciiAvg(char mystring[], int length); Write the function definition. The asciiAvg...
Given the following function prototypes: float asciiAvg(char mystring[], int length); Write the function definition. The asciiAvg function should return the average of the ascii values for the characters in the array mystring of length characters.
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
9.) Determine the focal length of a concave mirror given an object distance equal to 100.00...
9.) Determine the focal length of a concave mirror given an object distance equal to 100.00 cm and an image distance equal to 100.00 cm. a.) 37.50 cm b.) 87.50 cm c.) 50.00 cm d.) 22.75 cm Answer __________ 10.) The intensity of the light is 2200 watts/m2 at a distance of 1.50 x 1012 meter from a star. Determine the power output of the star. a.) 3.21 x 1026 watts b.) 6.22 x 1028 watts c.) 7.38 x 1026...
A regression between foot length (response variable in cm) and height (explanatory variable in inches) for...
A regression between foot length (response variable in cm) and height (explanatory variable in inches) for 31 students resulted in the following regression equation: y = 22 + 0.18 x. One student in the sample was 73 inches tall with a foot length of 29 cm. What is the predicted foot length for this student?
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
1. (13pts) Steel rods are manufactured with a mean length of 25 centimeters (cm). Because of...
1. (13pts) Steel rods are manufactured with a mean length of 25 centimeters (cm). Because of variability in the manufacturing process, the lengths of the rods are approximately normally distributed, with a standard deviation of 0.07 cm. (a) What proportion of rods have a length of exactly 25.07 cm? (b) What proportion of rods has a length less than 24.9 cm? (c) Any rods that are shorter than 24.85 cm or longer than 25.15 cm are discarded. What proportion of...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Given an integer named area which represents the area of a rectangle, write a function minPerimeter...
Given an integer named area which represents the area of a rectangle, write a function minPerimeter that calculates the minimum possible perimeter of the given rectangle, and prints the perimeter and the side lengths needed to achieve that minimum. The length of the sides of the rectangle are integer numbers. For example, given the integer area = 30, possible perimeters and side-lengths are: (1, 30), with a perimeter of 62 (2, 15), with a perimeter of 34 (3, 10), with...
The Setup: A piece of wire of length 10 cm is cut into two (not necessarily...
The Setup: A piece of wire of length 10 cm is cut into two (not necessarily equal length) pieces. One piece of length x cm is made into a circle and the rest is made into a square. 1. Express the sum of the areas of the square and circle as a function of x 2. For what x-value is maximum area achieved? 3. For maximum area, how should the wire be bent? 4. Find the critical values of your...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT