2012-04-10 93 views
1

轉換類型爲char

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]。

在此先感謝。

回答

5

要打開val成一個字符串,它show

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

不要忘記上showDict類型的約束:

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

我做了,但現在問題是'沒有實例(顯示)' – 2012-04-10 09:06:50

+4

如果'val'不是「可顯示」,爲它添加類似的實例。 – 2012-04-10 10:38:27

1

實例(顯示一個)=>展(字典一個)show = showDict

你必須告訴一個屬於showable類的類,否則你不能在val上使用show。