2

私は 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)

preorder 関数を定義すると、次のようなエラーが発生します。

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 関数定義で発生しました

preorder f g (Leaf x) = [x]
4

1 に答える 1