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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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