0

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

我的数据类型:

   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.
4

1 回答 1

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)
于 2012-04-21T10:25:49.997 回答