Question: Matrices in Haskell: type Matrix a = [[a]] Some examples are: -- 4 x 2 matrix eg1 :: Matrix Int eg1 = [ [1, 3],
Matrices in Haskell:
type Matrix a = [[a]]
Some examples are:
-- 4 x 2 matrix eg1 :: Matrix Int eg1 = [ [1, 3], [0, 5], [-3, 4], [2, 2] ] -- 2 x 3 matrix eg2 :: Matrix Int eg2 = [ [3, 1, 4], [-1, 0, 5] ] -- 0 x 0 matrix, an empty matrix eg3 :: Matrix Int eg3 = [ [] ] -- 2x1 matrix eg4 :: Matrix Int eg4 = [ [2], [3] ] eg5 :: Matrix Double eg5 = [ [6.2, 4.3, 7.4, -7.3], [9.3, 1.2, 0.4, -6.2] ]
need to build 10 funtions (first done):
1) empty :: Matrix a -> Bool
that returns True if the matrix is empty and False otherwise. For example:
> empty eg1 False > empty eg2 False > empty eg3 True
2) rowCount :: Matrix a -> Int
that returns the number of rows in the matrix. Hint: this is easy, except in the case of an empty matrix (the empty matrix will be a special case of many functions in this coursework so remember to consider that situation). For example:
> rowCount eg1 4 > rowCount eg2 2 > rowCount eg3 0
3) columnCount :: Matrix a -> Int
that returns the number of columns in the matrix. For example:
> columnCount eg1 2 > columnCount eg2 3 > columnCount eg3 0
4) element' :: Matrix a -> Int -> Int -> a
that returns the value in the matrix at the given row and column position. For this function you should assume that the row and column position will always be valid for the given matrix. For example:
> element' eg1 0 1 3 > element' eg1 2 0 -3 > element' eg2 1 1 0
5) element :: Matrix a -> Int -> Int -> Maybe a
that returns the value in the matrix at the given row and column position, or Nothing if that is not a valid position for the given matrix.
> element eg1 0 0 Just 1 > element eg1 0 1 Just 3 > element eg1 0 2 Nothing
6) data Either a b = Left a | Right b
data MatrixErr = NegIndex -- represents an index being negative | RowTooLarge -- represents a row index being too large for the matrix | ColTooLarge -- represents a column index being too large for the matrix deriving Show
define the function
elementEth :: Matrix a -> Int -> Int -> Either MatrixErr a
that returns the value in the matrix at the given row and column position, or a Left value with the appropriate error value from MatrixErr. Note: you will need to copy the definition of MatrixErr into your script. If multiple error conditions are true, you can return any of the appropriate errors.
> elementEth eg1 0 1 Right 3 > elementEth eg1 0 0 Right 1 > elementEth eg1 0 5 Left ColTooLarge > elementEth eg1 5 0 Left RowTooLarge > elementEth eg1 0 (-1) Left NegIndex
7) identity :: Int -> Maybe (Matrix Int)
that returns the Identity matrix for the given size. An Identity matrix is a square matrix which has zero for all values, except the values on the top-left to bottom-right diagonal which are all one. If the size is less than 1, then the identity matrix isn't defined and Nothing should be returned. For example:
> identity 2 Just [[1,0],[0,1]] > identity 5 Just [[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1]] > identity 0 Nothing > identity (-1) Nothing
8) addMat :: Num a => Matrix a -> Matrix a -> Maybe (Matrix a)
which takes two matrices and adds them together. Matrices may only be added together if they have the same number of rows and columns. If they do not, return Nothing. If they do, the values in the result matrix are the addition of the two values at the same positions in the two argument matrices. For example:
> addMat eg1 eg1 Just [[2,6],[0,10],[-6,8],[4,4]] > addMat eg1 eg2 Nothing > addMat eg3 eg3 Nothing
9) mapMat :: (a -> b) -> Matrix a -> Matrix b
that takes a function and a matrix and returns the matrix with the function applied to each value in the matrix. This should be the Matrix equivalent of map over a list. For example:
> mapMat (+1) eg5 [[7.2,5.3,8.4,-6.3],[10.3,2.2,1.4,-5.2]] > mapMat odd eg2 [[True,True,False],[True,False,True]] > mapMat (+1) eg3 [[]]
10) compress :: Num a => Matrix a -> Matrix a
that takes an r-by-c Matrix and returns a new r-by-1 Matrix where the values in each row have been replaced by a length 1 list containing the total value from adding together all the values in that row. For example:
> compress eg1 [[4],[5],[1],[4]] > compress eg5 [[10.599999999999998],[4.7]] > compress eg3 [[]]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
