Haskell Preview Trace Tree to List

I'm having trouble getting the code to do a pre-rollback tree traversal to the list for work. The definition for the tree is as follows:

data Tree a b = Branch b (Tree a b) (Tree a b)
          | Leaf a

and my definition for circumventing pre-orders is as follows:

preorder  :: (a -> c) -> (b -> c) -> Tree a b -> [c]
preorder f g (Leaf b) = [g b]
preorder f g (Branch a left right) = [f a] ++ preorder f g left ++ preorder f g right

However, the error I get is this:

Couldn't match type `b' with `a'
  `b' is a rigid type variable bound by
      the type signature for
        preorder :: (a -> c) -> (b -> c) -> Tree a b -> [c]
  `a' is a rigid type variable bound by
      the type signature for
        preorder :: (a -> c) -> (b -> c) -> Tree a b -> [c]
In the first argument of `f', namely `a'
In the expression: f a
In the first argument of `(++)', namely `[f a]'

I know that my problem is the type of the first argument of this function and how it should be of type [c], but I cannot understand how much I can get it. I tried all combinations of brackets around fa and without brackets, and none of them gave me a successful move.

+4
source share
1 answer

- , , , .

, Tree a b b , f preorder a. Leaf a, g , b.

: , f, b, a.

:

data Tree a b = Branch a (Tree a b) (Tree a b)
              | Leaf b

.

preorder

preorder  :: (a -> c) -> (b -> c) -> Tree a b -> [c]
preorder f g (Leaf a) = [f a]
preorder f g (Branch b left right) = [g b] ++ preorder f g left ++ preorder f g right
+4

All Articles