Question: Matrices in Haskell: type Matrix a = [[a]] i need to create a function: addMat :: Num a => Matrix a -> Matrix a ->
Matrices in Haskell:
type Matrix a = [[a]]
i need to create a function: 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
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] ]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
