Reference no: EM133257882
Use Ocaml
Fractions
Defined a type synonym for fractional value as followed:
type fraction = int * int
Also here is a function add to add fractions. An abbreviated version of the function from class is shown below:
let add ((n1,d1) : fraction) ((n2,d2) : fraction) : fraction =
(n1 * d2 + n2 * d1, d1 * d2)
Write a function named mul with the same type as add that will multiply two fractional values.
For example:
- mul (2,1) (3,1) evaluates to (6,1)
- mul (2,3) (3,4) evaluates to (6,12)
Next, write a function named simplify that takes a fraction as input and returns the reduced version that is mathematically equivalent but with the property that the only common divisor of the numerator and denominator is 1.
For example:
- simplify (2,6) evaluates to (1,3)
- simplify (1,4) evaluates to (1,4)
- simplify (2,3) evaluates to (2,3)
- simplify (mul (2,3) (3,4)) evaluates to (1,2)
If you use any functions that we developed in class to solve this problem then provide proper attribution in a comment.
Note that add and mul do not simplify their results, that task is left to simplify.
Tuples and Lists
Write a function compute_lengths that takes a list of strings as input and returns a list in which each string is paired with its length (as computed by String.length).
For example
- compute_lengths [ "Hello"; "Hi"; "" ] evaluates to [ ("Hello", 5); ("Hi", 2); ("", 0) ])
- compute_lengths [ ] evaluates to [ ]
Next, write a function named make_strings that takes a list of type (char * int) list and returns a string list in which for each pair in the list c and i a string is constructed by repeating the character c, i times. Experiment with the function String.make to see how to use it for this purpose.
For example
- make_strings [ ] evaluates to [ ]
- make_strings [ ('a', 3); ('x', 0); ('4', 4) ] evaluates to [ "aaa"; ""; "4444" ]
Points, distances, triangles, and perimeters
To represent geometric points on a 2-dimentional plane we may define the following type synonym:
type point = float * float
The first element of the pair indicates a point's position on the x-axis and the second, its position on the y-axis.
Define a function distance of type
point -> point -> float
that computes the distance between the 2 input points. You may find the functions Float.abs and Float.sqrt to be useful. Again, experiment with them in utop to fully understand their behavior.
For example:
- distance (1.0, 1.0) (2.0, 2.0)) evaluates to 1.414
- distance (2.0, 2.0) (1.0, 1.0) evaluates to 1.414
- distance (1.0, 1.0) (3.0, 3.0) evaluates to 2.828
- distance (2.0, 3.5) (0.2, 4.5) evaluates to 2.059
- distance (-1.0, -1.0) (0.0, 0.0) evaluates to 1.414
Next, we define a type synonym for triangles as being a triple of 3 points as follows:
type triangle = point * point * point
Now write a function named perimeter that computes the perimeter of a triangle. This is the distance "all the way around" the triangle and can be implemented by adding the distance between all 3 pairs of connected points in the triangle.
For example:
- eqf (perimeter ((1.0, 1.0), (3.0, 1.0), (3.0, 3.0))) 6.828 evaluates to true
- eqf (perimeter ((1.0, 1.0), (3.0, 2.0), (2.0, 3.0))) 5.886 evaluates to true
Finally, write a function named triangle_perimeters that computes a list of perimeters when given a list of triangles. Feel free to use the perimeter function you just implemented.
For example:
- triangle_perimeters [ ((1.0, 1.0), (3.0, 1.0), (3.0, 3.0)); ((1.0, 1.0), (3.0, 2.0), (2.0, 3.0)) ] evaluates to [ 6.828; 5.886 ]