Defined a type synonym for fractional value

Assignment Help Computer Engineering
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 ]

Reference no: EM133257882

Questions Cloud

Compare and contrast the capabilities : CS 104 City School of Law, Quetta Compare and contrast the capabilities of each of the software found. use Cellebrite UFED for phone and printed circuit boards
The formula in getting the p-stat is tdist : The formula in getting the p-stat is TDIST (t-stat, degrees of freedom, tails).
What your position as a leader is on the Leadership Grid : For this leadership journal entry, determine and relate what your position as a leader is on the Leadership Grid (pp. 230-231)
Show that details of a computing system can impact : What if it the cache is 10 times faster than RAM, 40 times faster? Discuss (there is no need for detailed calculations) whether the break-even point, N
Defined a type synonym for fractional value : Defined a type synonym for fractional value - Write a function compute_lengths that takes a list of strings as input and returns a list in which each string
Identify the company and describe its industry : Identify the company and describe its Industry and its products or services. Describe the Change or changes that occurred
Discuss the statement and give your scientific opinion : Discuss the statement and give your scientific opinion with some of the details - It may include redundant information, such as length of the list
What should you place in the blank : CS 12S San Francisco State University What should you place in the blank within the following JavaScript code to assign an object with information
Describe why you think it is defamatory : BA 636 Campbellsville University propose a method by which people who perceive they are harmed by such defamatory postings should be able to seek legal redress

Reviews

Write a Review

Computer Engineering Questions & Answers

  Develop an eer model for the given problem description

House2Home Franchise Application Database System. You are required to develop an EER model for the above problem description.

  Discuss the benefits of virtualization software

Discuss the benefits of virtualization software, as described in the text. Do you agree/disagree with these benefits, or can you think of additional benefits.

  Create a gui pad that has numbers and letters

Create a GUI pad that has numbers and letters(That can be capitalized), and a text area to display One button that clears the text.

  Explain details that should be included in your cloud sla

Discuss in 500 words or more the top 5 details that should be included in your cloud SLA. Use at least three sources. Use the Research Databases available.

  Create an archive named foobar a containing the object files

create an archive named foobar.a containing the object files foo1.o and foo2.o. delete foo2.o from the archive. Can this archive be used with -l option to cc?

  Write a source code in java nxt legos for this lawn mower

Motor A is the blade which is constantly spinning. Motor B and C are the whelels. The robot has 2 sensors. Touch sensor and light sensor.

  Difference between hardness and toughness

1. The relationship between heat treatment and toughness and hardness i.e. briefly explain why the different heat treatments give different toughness and hardness values. What do you think is the difference between hardness and toughness?

  Discuss bus, ring and star topologies

summary about each of the following topologies; bus, ring and star topologies

  Compute the surface area and volume of a sphere

The program should prompt the user for the radius and then print out the surface area and volume.

  Summarize the position of the two major camps

Summarize the position of the two major camps and explain the root cause of the division. (Hints: I refer to the two camps as "strong" and "weak;" the cause deals with the nature of "I" in AI.)

  Explain the theories of operations security

ISOL631– Operations Security University of the Cumberlands Provide a reflection of at least 500 words (or 2 pages double spaced) of how the knowledge, skills.

  Cores on computer systemssingle-processor computers were

cores on computer systemssingle-processor computers were able to handle and process smaller sets of data. as

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd