1

我想打印一个矩阵。例如:

       data Matrix = Matr [[Int]]

       instance Show Matrix where
                show(Matr (x:xs)) = show(x)++"\n"++show(head(xs))

       Example of use:

                Matr [[1,2,3],[4,5,6],[7,8,9]]
                [1,2,3]
                [4,5,6]
                [7,8,9] -- this line is not showed on my instance Show 

如何显示矩阵中的所有元素?谢谢。

4

1 回答 1

4

您必须遍历所有元素

instance Show Matrix where
  show (Matr d) = print d
    where print [] = []
          print (x:xs) = show x ++ "\n" ++ print xs

其他方式

instance Show Matrix where
  show (Matr d) = concatMap ((++"\n").show) d
于 2012-12-04T10:20:40.240 回答