Apply a function to each element of the matrix

I am trying to apply a function to a matrix, but I do not know how to do it.

This is how I define my matrix:

data Matrice a = Mat [[a]]

montre [] = "/"
montre (t:q) = "" ++ (Pp.printf "%5s" (show t)) ++ " " ++ (montre q)

instance (Show a) => Show (Matrice a) where
        show (Mat ([])) = ""
        show (Mat (t:q)) = "/" ++ (montre t) ++ "\n" ++ (show (Mat q))

Then, once my matrix is ​​defined, I would like to apply my function z95to each of the elements of the matrix.

Here is the signature of my function z95(which allows you to convert an integer to this integer modulo 95)

z95 n = Z95(n %% 95)
z95 18 = 18%95

I tried to make double mapaccess to the elements of my matrix, but then I did not understand how to apply my function z95.

Thanks for the help!

+4
source share
1 answer

Functor , .

instance Functor Matrice where
  fmap f (Mat xss) = Mat (map (map f) xss)

>> let m = Mat [[1,2,3],[4,5,6]]
>> fmap (+3) m -- => Mat [[4,5,6],[7,8,9]]

>> fmap z95 m
+7

All Articles