listTree :: Tree a -> [a]
listTree = foldTree f z
where
f x y z = x y z
z = []
This is what I have so far, but my f is wrong because the expected type is Tree a -> [a]
but the actual is Tree [a] -> [a]
data Tree a
= Tip
| Bin (Tree a) a (Tree a)
deriving (Show, Eq)
foldTree :: (b -> a -> b -> b) -> b -> Tree a -> b
foldTree f z Tip = z
foldTree f z (Bin l x r) = f (foldTree f z l) x (foldTree f z r)
This is fold and data type for tree.
Need help with defining f
.
CodePudding user response:
First of all, combining lists uses
in haskell.
is only used for adding numbers. Second, the variable y
is of type a
, not [a]
, so you cannot use
on it directly.
listTree :: Tree a -> [a]
listTree = foldTree f z
where
f x y z = x [y] z
z = []