没有因使用“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的元素,但是不是说那个类型aShow 的实例,因此您需要在 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的一个实例givenaShow的一个实例。对于您给定的示例数据,我们将获得:

Prelude> Node (Leaf 1) (Node (Node (Leaf 3) (Leaf 4)) (Node (Leaf 8) (Leaf 7)))
<{1},<<{3},{4}>,<{8},{7}>>>