Trouble adding matrices together

Viewed 45

I'm having trouble compiling the following:

data Matrix = Matrix [[Float]] deriving (Eq)

aditionMat :: Matrix -> Matrix -> Matrix
aditionMat m1 m2 = Matrix $ zipWith (zipWith (+)) m1 m2

I'm working on defining a custom matrix data type, but the method I made for adding two matrices together won't work. aditionMat works fine when I give it two [[Float]], but won't compile when I feed it two Matrix. The error is the following:

Couldn't match expected type [[Float]] with actual type Matrix (for both m1 and m2)

Apologies for the very trivial question but I can't see why it fails

1 Answers

m1 and m2 are Matrixes, not [[Float]]s, so you can not use zipWith on these. You need to unwrap the list of lists from the Matrix data constructor:

aditionMat :: Matrix -> Matrix -> Matrix
aditionMat (Matrix m1) (Matrix m2) = Matrix $ zipWith (zipWith (+)) m1 m2
Related