我跟着書來定義Tree數據類型,但是show不能正常工作。爲什麼?Haskell爲什麼不能推斷Tree類型?
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
test = show EmptyTree
提供錯誤消息:
No instance for (Show a0) arising from a use of ???show???
The type variable ???a0??? is ambiguous
Note: there are several potential instances:
instance Show a => Show (Tree a)
-- Defined at /Users/gzhao/Documents/workspace/hsTest2/src/Tree.hs:3:62
instance Show Double -- Defined in ???GHC.Float???
instance Show Float -- Defined in ???GHC.Float???
...plus 25 others
In the expression: show EmptyTree
In an equation for ???test???: test = show EmptyTree
你是如何運行的代碼?你在使用解釋器(ghci)嗎?這一行:'test = show EmptyTree'不是有效的Haskell語法,所以你可能不會粘貼你的所有代碼。 – rburny 2014-09-21 18:19:38
@rburny我認爲OP在源文件中有。 – Sibi 2014-09-21 18:20:09
我正在使用Eclipse – 2014-09-22 01:20:06