不符合预期的类型

时间:2012-04-21 10:20:50

标签: haskell

如何解决此错误并使我的代码正常工作?

我的数据类型:

   data ID x = ID ( ( x, Mark ), [ ID x ] ) 
   data Mark = Marked | Unmarked

显示实例:

  instance  Show a  => Show ( ID a )  where

           show t  = go " " t   where 

                  go aP ( ID ( (x, Marked ), z ) ) =

                           aP ++ x ++ "\n" 

错误:

Couldn't match expected type `[Char]' against inferred type `a'
  `a' is a rigid type variable bound by
      the instance declaration at Dictionary.hs:117:23
  Expected type: ID [Char]
  Inferred type: ID a
In the second argument of `go', namely `t'
In the expression: go "" t
Failed, modules loaded: none.

1 个答案:

答案 0 :(得分:4)

编辑:

我发现你的意图难以破译,但我想也许你想把树形结构表示为一个字符串?这是一个演示实现,向您展示如何完成它(以快速,黑客的方式。)

data ID x = ID ((x, Mark), [ID x]) 
data Mark = Marked | Unmarked

instance Show a => Show (ID a) where
    show (ID ((x, _), ids)) = "Val: " ++ show x ++
                              ", Children: " ++ 
                              (show $ map show ids)