This is a homework assignment first off. We are given a newtype Matrix which is the professor's implementation of an abstract Matrix. My main issue is how do you create a list of type Matrix. The first function fillWith which I need to implement takes a tuple which is (number of rows, number of columns) of the matrix to create and the data to place at each index.
module Matrix (Matrix, fillWith, fromRule, numRows, numColumns,
at, mtranspose, mmap, add, mult)
where
-- newtype is like "data", but has some efficiency advantages
newtype Matrix a = Mat ((Int,Int),(Int,Int) -> a)
--fillWith :: (Int,Int) -> a -> (Matrix a)
--fillWith ix val = Mat ((,
--trying to create a row of type Matrix
row_matrix :: [a] -> Matrix a
row_matrix ls = Mat ((1, length ls), (\x y -> if x > 1 then undefined else ls !! (y-1)))
You are just missing some parens and a comma:
row_matrix ls = Mat ((1, length ls), (\(x,y) -> if x > 1 then undefined else ls !! (y-1)))
^-^-^--- add these
Mat
is a tuple where:
You may always use a where
or let
clause to simplify the construction of values, e.g.:
row_matrix as = Mat (bounds, accessor)
where bounds = (1, length as)
accessor (i,j) = if i > 1 then undefined else as !! (j-1)
Using a where
clause makes the code a lot more readable.
To implement fillWith
I would follow the same recipe:
fillWith bounds val = Mat (bounds, accessor)
where accessor (i,j) = ???
I think it's obvious now what the ??? should be.