1

我有

data Dictionary a = Empty
                  | Branch (a, Bool, Dictionary a) (Dictionary a)
                  deriving (Ord, Eq)

instance Show (Dictionary a) where show = showDict

showDict (Branch (val, e, dict) dict2) = "My dict is : " ++ val 

我知道这绝对是错误的,但我找不到如何写这个。在 showDict 函数中,val 的类型是一个但预期的类型是 [Char]。

提前致谢。

4

2 回答 2

5

val变成一个字符串,show它:

showDict (Branch (val, e, dict) dict2) = "My dict is : " ++ show val

并且不要忘记类型约束showDict

instance Show a => Show (Dictionary a) where show = showDict
于 2012-04-10T08:48:48.247 回答
1

instance (Show a) => Show (Dictionary a) where show = showDict

你必须告诉 a 属于可展示的类型类,否则你不能在 val 上使用 show 。

于 2012-04-10T11:07:20.710 回答