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 に答える