1
我在創建一個函數來打印樹的內容時遇到了一些麻煩。我對數據類型定義樹爲:無法證明顯式鍵入的約束綁定錯誤 - 樹形打印
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
打印樹的代碼是:
printTree :: Tree a -> String
printTree (Node a left right) = printTreeAux (Node a left right) 0
printTreeAux :: Tree a -> Int -> String
printTreeAux EmptyTree _ = ""
printTreeAux (Node a left right) depth = (replicate (6 * depth) ' ') ++ show a ++ "\n" ++ (printTreeAux left (depth + 1)) ++ "\n" ++ (printTreeAux right (depth + 1))
而且我得到加載文件時擁抱了以下錯誤:
ERROR file:.\Tree.hs:30 - Cannot justify constraints in explicitly typed binding
*** Expression : printTreeAux
*** Type : Tree a -> Int -> String
*** Given context :()
*** Constraints : Show a
我已經搜索了一些澄清,但沒有發現任何能真正幫助... 在此先感謝。
它沒有工作,謝謝。 – user1898820 2014-12-04 20:51:59