没有因使用“show”而产生的(Show a)实例
No instance for (Show a) arising from a use of ‘show’
没有因使用“show”而产生的 (Show a) 实例
在‘(++)’的第一个参数中,即‘show a’
data LTree a = Leaf a | Node (LTree a) (LTree a)
instance Show (LTree a) where
show (Leaf a) = "{" ++ show a ++ "}"
show (Node fe fd) = "<" ++ (show fe)++ "," ++(show fd)++ ">"
Node (Leaf 1) (Node (Node (Leaf 3) (Leaf 4)) (Node (Leaf 8) (Leaf 7)))
我应该得到:
<{1},<<{3},{4}>,<{8},{7}>>>
在你的行中:
show (Leaf a) = "{" ++ <b>show a</b> ++ "}"
你调用show a
,用a
一个类型a
的元素,但是不是说那个类型a
是 Show
的实例,因此您需要在 instance
声明中添加约束:
instance <b>Show a =></b> Show (LTree a) where
show (Leaf a) = "{" ++ show a ++ "}"
show (Node fe fd) = "<" ++ (show fe)++ "," ++(show fd)++ ">"
所以这里我们说LTree a
是show的一个实例givena
是Show
的一个实例。对于您给定的示例数据,我们将获得:
Prelude> Node (Leaf 1) (Node (Node (Leaf 3) (Leaf 4)) (Node (Leaf 8) (Leaf 7)))
<{1},<<{3},{4}>,<{8},{7}>>>
没有因使用“show”而产生的 (Show a) 实例 在‘(++)’的第一个参数中,即‘show a’
data LTree a = Leaf a | Node (LTree a) (LTree a)
instance Show (LTree a) where
show (Leaf a) = "{" ++ show a ++ "}"
show (Node fe fd) = "<" ++ (show fe)++ "," ++(show fd)++ ">"
Node (Leaf 1) (Node (Node (Leaf 3) (Leaf 4)) (Node (Leaf 8) (Leaf 7)))
我应该得到:
<{1},<<{3},{4}>,<{8},{7}>>>
在你的行中:
show (Leaf a) = "{" ++ <b>show a</b> ++ "}"
你调用show a
,用a
一个类型a
的元素,但是不是说那个类型a
是 Show
的实例,因此您需要在 instance
声明中添加约束:
instance <b>Show a =></b> Show (LTree a) where
show (Leaf a) = "{" ++ show a ++ "}"
show (Node fe fd) = "<" ++ (show fe)++ "," ++(show fd)++ ">"
所以这里我们说LTree a
是show的一个实例givena
是Show
的一个实例。对于您给定的示例数据,我们将获得:
Prelude> Node (Leaf 1) (Node (Node (Leaf 3) (Leaf 4)) (Node (Leaf 8) (Leaf 7)))
<{1},<<{3},{4}>,<{8},{7}>>>