In: Computer Science
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.)
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; }