我对 Haskell 相当陌生,并且正在尝试完成我班上的作业。我正在尝试创建一个预排序函数来遍历以下格式的树对象
preorder :: (a -> c) -> (b -> c) -> Tree a b -> [c]
preorder f g Empty = []
preorder f g (Leaf x) = [x]
我的树类如下
data Tree a b = Empty | Leaf b | Branch a (Tree a b) (Tree a b)
在定义预购函数时,我得到如下所示的错误。
Couldn't match expected type 'c' with actual type 'b'
'c' is a rigid type variable bound by the type signature for preorder :: (a -> c) -> (b -> c) -> Tree a b -> [c].
'b' is a rigid type variable bound by the type signature for preorder :: (a -> c) -> (b -> c) -> Tree a b -> [c].
它们发生在最后一个预购函数定义中
preorder f g (Leaf x) = [x]