List 在Haskell中将show与列表一起使用

List 在Haskell中将show与列表一起使用,list,haskell,types,List,Haskell,Types,我在使用show打印列表列表给出的矩阵行时遇到一些问题 我有这个: data Matrix = Mat Int [[Bit]] deriving Eq 其中,参数Int是平方矩阵的阶数,Bit是Int(0或1)。我需要我的代码能够使用Matrix作为Show的实例执行以下操作: Main> Mat 3 [[0,0,1],[1,0,1],[1,1,1] [0,0,1] [1,0,1] [0,0,1] 到目前为止,我只有: instance Show Matrix where

我在使用
show
打印列表列表给出的矩阵行时遇到一些问题

我有这个:

data Matrix = Mat Int [[Bit]] 
    deriving Eq
其中,参数
Int
是平方矩阵的阶数,
Bit
是Int(0或1)。我需要我的代码能够使用
Matrix
作为
Show
的实例执行以下操作:

Main> Mat 3 [[0,0,1],[1,0,1],[1,1,1]
[0,0,1]
[1,0,1]
[0,0,1]
到目前为止,我只有:

instance Show Matrix where
    show (Mat i (x:xs)) = (show x) ++ "\n"
但这显然只返回第一个列表。你能帮我解决这个问题吗?
提前感谢。

简单的方法是
显示所有行,并将它们分别放在各自的行中:

instance Show Matrix where
    show (Mat _ rows) = unlines $ map show rows
这样做的一个小缺点是,它还在最后一行之后添加了一个换行符,为了避免这种情况,您可以使用

instance Show Matrix where
    show (Mat _ rows) = intercalate "\n" $ map show rows

(需要导入
数据。对于
插入
,请列出

简单的方法是
显示所有行,并将它们分别放在各自的行上:

instance Show Matrix where
    show (Mat _ rows) = unlines $ map show rows
这样做的一个小缺点是,它还在最后一行之后添加了一个换行符,为了避免这种情况,您可以使用

instance Show Matrix where
    show (Mat _ rows) = intercalate "\n" $ map show rows
(需要导入
数据。对于
插入
,列表