Question

In: Computer Science

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. (Reminder: 2.54 cm per inch.)

Solutions

Expert Solution

First of, float is a datatype like any other available (like Int, Char, Double etc.).

Int = Integer (e.g. 4 or 10)

Float = Floating number up to 7 decimals (e.g. 1.0054 or 10.55555)

Double = Is also a floating number but up to 2 times larger: up to 15 or 16 decimals. (e.g. 1.5353415335)

Char = A Character (e.g. 'a' or 'G').

These are the most popular datatypes.

What you want to do in some programming languages like Objective-C in this case; is to tell the compiler what type the variable will have. If you're going to give the variable x = 1 then you must tell the compiler before you do that. You will need to change to declaration to: int x = 1. The same rules follows with the float and other datatypes.

An argument to a function is like Math in high-school. (example):

f(x) = 5x + 1.

or

f(x,y) = x + y.

The last function take two arguments: x and y.

The same logic is used in programming except that you tell the compiler what type the function will return.

f(x,y) = x + y

Will be in C-programming if you use Integers:

int f(int x, int y) { return x + y }

Your answer will probably be:

(in C):

float addTwo(float first, float second) {

    return first + second;

}

(in Objective-C):

- (float)addTwo:(float)first secondArgument:(float)second  {

    return first + second;

}

Related Solutions

Suppose you're given a square sheet of tinfoil with side length 9 inches. Let us make...
Suppose you're given a square sheet of tinfoil with side length 9 inches. Let us make a box by removing identical squares of length x from each corner of the sheet, and folding up the resulting flaps. Find the dimensions of the box with the largest volume by optimizing over x. Enter an exact value. Height of the Box=x= Length=Width=
For a rhombus with diagonals of length 6 cm and 8 cm.
For a rhombus with diagonals of length 6 cm and 8 cm. (a) Find the length of each side of the rhombus. (b) Find the area of the rhombus. (c) Explain why a circle can be inscribed inside a rhombus (any rhombus). A circle inscribed in a polygon means each side of the polygon is tangent to the circle. Use what you know about why any triangle has an inscribed circle. (d) Find the exact area of the inscribed circle in terms of π.
A strip of elastomer 1 cm×1 cm×10 cm is stretched to 25 cm length at T...
A strip of elastomer 1 cm×1 cm×10 cm is stretched to 25 cm length at T =25C, a stress of 1.5 MPa being required (a) Assuming a tetrafunctional crosslinking mode, how many moles of network chains are there per cubic centimeter? (b) What stress is required to stretch the sample to only 15 cm at T = 25C? (c) What stress is required to stretch the sample to 25 cm length at 100C?
A 320 turn solenoid with a length of 19.0 cm and a radius of 1.20 cm...
A 320 turn solenoid with a length of 19.0 cm and a radius of 1.20 cm carries a current of 1.90 A. A second coil of four turns is wrapped tightly around this solenoid, so it can be considered to have the same radius as the solenoid. The current in the 320 turn solenoid increases steadily to 5.00 A in 0.900 s. (a) Use Ampere's law to calculate the initial magnetic field in the middle of the 320 turn solenoid....
The tube length in a certain compound microscope is 25 cm. The focal length of the...
The tube length in a certain compound microscope is 25 cm. The focal length of the objective lens is 2.1 cm. The clearest image is formed when an object of height 1 mm is placed at a distance of 2.3 cm from the objective lens. Determine: (i) The position of the intermediate image, (ii) the focal length of the eye piece, that will place the final image at the near-point, (iii) the total magnification of the microscope and the height...
A rectangular block of copper has sides of length 12 cm, 26 cm, and 37 cm....
A rectangular block of copper has sides of length 12 cm, 26 cm, and 37 cm. If the block is connected to a 5.4 V source across two of its opposite faces, find the following. (a) What is the maximum current the block can carry? (b) What is the minimum current the block can carry?
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] );...
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] ); Argument pt[2] defines a point on the plane: pt[0] is the x-coordinate, pt[1] is the y-coordinate. Argument rect[4] defines a rectangle on the same plane. rect[0] and rect[1] define the x- and y- cordinates respectively of one corner of the rectangle. rect[2] and rect[3] define the opposite corner. Coordinates may be any valid floating point value, including negative values. The function returns int 0...
A cylindrical shaped potato with length 2 cm and a diameter of 0.5 cm was dried...
A cylindrical shaped potato with length 2 cm and a diameter of 0.5 cm was dried at a temperature of 65 o C in a convective oven for 24 h, if the surface temperature of the potato remained at 62 o C throughout the drying operation, describe the different changes that occurs during the constant and falling rate periods
Suppose that a triangle has sides of length a = 15 cm, b = 37 cm,...
Suppose that a triangle has sides of length a = 15 cm, b = 37 cm, and c = 29 cm, then α = _______, β = ______., and γ =______ . Carefully round your answers to the nearest tenth of a degree, do not include units in your typed answers.
A solenoid of radius 3 cm, 2900 turns, and a length of 8 cm is carrying...
A solenoid of radius 3 cm, 2900 turns, and a length of 8 cm is carrying a current that alternates according to the formula I(t)=I0cos(2πft) where the peak current is I0=1200 mA and the frequency is f=100 Hz. A.) What is the RMS EMF induced by the fluctuating current? B.) What is the RMS strength of the electric field that corresponds to the induced EMF? C.) What is the RMS electric field strength at a distance 6 cm from the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT