0
我有一個二叉樹的定義在Haskell如下所示:二叉樹折功能
data BTree x = Nil | BNode x (BTree x) (BTree x)
我再爲這個數據類型摺疊的定義:
foldB :: (x -> u -> u -> u) -> u -> BTree x -> u
foldB f a Nil = a
foldB f a (BNode x l r) = f x (foldB f a l)(foldB f a r)
所以我希望,我可以簡單地使這個函數總和所有的值:
sumBFold :: (Num a) => BTree a -> a
sumBFold x = foldB (+) 0 x
但這不起作用,我不能爲我的生活找出原因。 我收到錯誤消息的有用的部分是:
Couldn't match type `a` with `a -> a'
`a' is a rigid type variable bound by the type signature for:
sumBFold :: forall a. Num a => BTree a -> a
Expected type: (a -> a) -> a -> a -> a
Actual type: (a -> a) -> (a -> a) -> a -> a
In the first argument of folB namely `(+)`
您需要傳遞給'foldB'的函數需要3個參數,而'(+)'只需要2個參數。 – Lee