我試圖構建一個功能打印二叉樹,所以我這樣做:我怎樣才能打電話給一個show instace打印一棵二叉樹?
instance Show a => Show (Tree a) where
show Null = "_"
show (Nod x e d) = "(" ++ show x ++ " " ++ show e ++ " " ++ show d ++ ")"
left :: Tree a -> Tree a
left (Nod x e d) = show e
,但我收到此消息:
Couldn't match type `[Char]' with `Tree a' Expected type: Tree a Actual type: String In the return type of a call of `show' In the expression: show e In an equation for `left': left (Nod x e d) = show e Failed, modules loaded: none.
提示:你的'left'函數的結果類型應該是什麼,你寫的類型簽名是怎麼說的? – duplode
我試圖弄到樹的左側,所以我收到一棵樹,並返回另一棵樹的左側子樹 – Marcio
如果您只想「left」返回左側子樹,則不需要使用「在其中顯示''show'產生一個'String'。 – duplode