Question: Code in Ocaml please NOT assembly 3. Vector functions Let's finish this lab exercise by writing a few functions that deal with 2-dimensional vectors, represented

 Code in Ocaml please NOT assembly 3. Vector functions Let's finish

Code in Ocaml please NOT assembly

3. Vector functions Let's finish this lab exercise by writing a few functions that deal with 2-dimensional vectors, represented as pairs of float s. (As a reminder, a 2-dimensional vector is basically just an (x,y) point in space: its length is its distance from the origin - (0,0)) Create a file named vector.ml in your labi directory. Open vector.ml in the text editor and try writing OCaml definitions for the following functions: scale Scalar multiplication of a vector by a real number simply multiplies both components by the scalar. Write a definition for scale : float -> float * float -> float * float so that: scale 3. (1., 2.) evaluates to (3., 6.) and scale 2. (-1.,4.) evaluates to (-2.,8.). . length The length of a vector (a,b) is the square root of a* a + b*b. So if we define length : (float*float) -> float we should have: length (3.,4.) should evaluate to 5. length (5., 12.) should evaluate to 13. . (There is an OCaml function, sort : float -> float, that computes the square root of a floating-point number.) vec_add We can use OCaml's tuples to represent two-dimensional vectors as pairs of float s. Write the OCaml definition for the function vec_add : (float * float) > (float * float) -> (float*float) that adds two vectors together. Some sample evaluations: . vec_add (1.0,2.5) (3.0,-1.0) evaluates to (4., 1.5) vec_add (0.0, 1.0) (2.0,2.0) evaluates to (2., 3.) . dot Recall that the dot-product or inner product of two vectors is the sum of the component-wise products, e.g. in "math" the dot product of (a,b) and (c,d) is axc+bxd. Write the OCaml definition for the function dot : (float * float) -> (float * float) -> float to compute the dot product of two 2-dimensional vectors. Some sample evaluations: dot (0.0,2.0) (1.0,3. 14) evaluates to 6.28 dot (1.0,-1.0) (3.0, 4.0) evaluates to -1. perp Two vectors are perpendicular or orthogonal if their inner product is 0. Write the OCaml definition for the function perp: float*float -> float*float -> bool that returns true if its arguments are perpendicular and false otherwise. Sample evaluations: perp (0.,1.) (-1.,0.) evaluates to true perp (1.,2.) (-1.,1.) evaluates to false For implementation simplicity, assume that the O-vector (0.,0.) is perpendicular to any vector, including itself

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!