我有一個數據類型:爲什麼我會遇到這些類型錯誤?
data Tree = Empty | Node Int Tree Tree
,我想功能
nodeDepth :: Tree -> [(Int, Int)]
一對用於每個節點。第一個元素是標籤(值),第二個元素是深度。
我的意圖(原始代碼)是這樣的:
nodeDepth (Node label left right) = zip nodeDepth' (Node label left right) [0]
nodeDepth' Empty _ = []
nodeDepth' (Node label left right) [level] = label : nodeDepth' (Node label left right) level : (1 + level)
但是,這是行不通的。
有什麼不對?我使用的弗雷格REPL
Error message are like :
E <console>.fr:22: t19906 occurs in type [t19906] rendering expression level{19727} untypable.
E <console>.fr:22: type error in expression level
type is t19906
used as [t19906]
E <console>.fr:22: type error in expression
nodeDepth' (Node label left right) level:+ 1 level
type is [[t19909]]
used as [Int]
E <console>.fr:22: [[Int]] is not an instance of Num
E <console>.fr:20: type error in expression nodeDepth'
type is apparently [t19961]
used as function
H <console>.fr:20: too many or too few arguments perhaps?
E <console>.fr:20: type error in expression Node label left right
type is Tree
used as [t19964]
E <console>.fr:20: type error in expression
zip nodeDepth' (Node label left right)
type is apparently [(t19961,t19964)]
used as function
H <console>.fr:20: too many or too few arguments perhaps?
W <console>.fr:20: application of nodeDepth will diverge.
弗雷格[與常規哈斯克爾不同](https://github.com/Frege/frege/wiki/Differences-between-Frege-and-Haskell);我認爲使用[tag:frege]標籤而不是[tag:haskell]更合適。 – Zeta
您無法獲取節點的深度。你只能得到它的高度(例如離樹葉最遠的距離)那麼你想得到什麼? – gen
@Zeta感謝您的編輯。如果他使用ghci,類型錯誤將會是同構的。但沒關係,我們關心這一點。 :) – Ingo