運行摺疊(+)0示例給我一個關於(+)應用於太多參數的錯誤。爲什麼?BST摺疊函數的通用版本中的參數太多
data(Ord a, Show a, Read a) => BST a = Void | Node {
val :: a,
left, right :: BST a
} deriving (Eq, Ord, Read, Show)
sample = Node 5 (Node 3 Void Void) (Node 10 Void Void)
fold :: (Read a, Show a, Ord a) => (a -> b -> b -> b) -> b -> BST a -> b
fold _ z Void = z
fold f z (Node x l r) = f x (fold f z l) (fold f z r)
Occurs check: cannot construct the infinite type: a = a -> a Probable cause: `+' is applied to too many arguments In the first argument of `fold'', namely `(+)' In the expression: fold' (+) 0 sample
參見:fold
你可能想看看讓你的類型的實例[可摺疊](http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Foldable.html#t:Foldable)和[Traversable](http://hackage.haskell.org/packages /archive/base/latest/doc/html/Data-Traversable.html#t:Traversable) – 2011-02-04 03:18:04