Practice: Haskell Basics
Exercises
- Create a file called VectorMath.hs .
- Inside the VectorMath.hs file, provide a header documentation that provides the name of the file, your name, and a description of whats inside the file. For example:
{- File : VectorMath.hs Copyright : (c) Lamont Samuels, 09/24/17 Contains a type alais for representing two-component vectors and functions working on them. -}
- Next, delcare a type alias called Vec2 that is a synonym for a tuple of two
Double
values:{- type alais for representing a 2D Vector as a tuple -} type Vec2 = (Double, Double)
- Now you'll implement functions that will work on
Vec2
and other types. Details for each function are given in the descriptions that follow. If interested, you will find additional discussion of vectors at http://en.wikipedia.org/wiki/Euclidean_vectorNote: Make sure to provide a type signature for ALL functions inside your vector file- scale : This function returns a
Vec2
with its components equal to the original vector but scaled (i.e., multiplied) by the scalar argument.Prelude> scale (1.0,2.0) 1.5 (1.5,3.0)
- dot : This function performs a type of multiplication (product) on vectors. The returned value is a scalar real number.
The dot product of two vectors is computed as follows.
(x1, y1) * (x2, y2) = x1 * x2 + y1 * y2
Prelude> dot (2.5,5.6) (56.4, 34.5) 334.2
- vec2Length : The length of a vector (i.e., its magnitude) is computed from its components using the Pythagorean theorem.
Prelude> vec2Length (3,4) 5.0
- normalize : The function creates (and returns) a new vector by normalizing the input vector. This means that the resulting vector has the same direction but a magnitude of 1. In short, the new vector is the original vector scaled by the inverse of its length.
Here's a link to help you: http://www.fundza.com/vectors/normalize/
Main> normalize (12.0, -5.0) (0.9230769230769231,-0.38461538461538464)
- scale : This function returns a