Question: Matrices in Haskell: type Matrix a = [[a]] i need to create two functions: 1) element' :: Matrix a -> Int -> Int -> a
Matrices in Haskell:
type Matrix a = [[a]]
i need to create two functions:
1) 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
2) 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
some examples:
-- 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
