2012-04-21 90 views
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. 
+0

你能描述你希望你的秀ID例如做什麼? – Sarah 2012-04-21 10:22:48

+0

@Sarah我試圖展示這棵樹。但是,對於這種情況,我將數字存儲爲字符。看到樹,看看http://stackoverflow.com/questions/10244343/visiting-each-node – john 2012-04-21 10:24:47

+0

執行'go'與這個問題相關 – huon 2012-04-21 10:25:57

回答

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) 
+0

Thankssssss .... – john 2012-04-21 10:38:13

相關問題