I have got a tree and I need the depth of the tree, but it doesn't work.
data Tree a b = Leaf a | Branch (b,Tree a b) (b,Tree a b) deriving(Eq,Show)
tree = Branch ("A",Branch("C",Leaf 3)("D",Branch("G",Leaf 7)("H",Leaf 6)))("B",Branch("E",Leaf 5)("F",Leaf 4))
My function:
depth (d,Leaf x) = 1
depth (d,Branch a b) = 1 + (max (depth a) (depth b))
Couldn't match expected type `(t10, Tree t20 t10)'
with actual type `Tree Integer [Char]'
In the first argument of `depth', namely `tree'
In the expression: depth tree
In an equation for `it': it = depth tree
(b,Tree a b)
rather than just(Tree a b)
)? – duplodeBranch
is the essentially same asBranch b (Tree a b) (Tree a b)
:Branch (b,Tree a b) (b,Tree a b) ~= Branch b (Tree a b) b (Tree a b) ~= Branch (b, b) (Tree a b)
. If we havedata NewTree = Leaf a | Branch b (Tree a b) (Tree a b)
, we can get the original tree withtype OriginalTree a b = NewTree a (b, b)
. – David Young