My tree is defined by
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Show)
I also declare a testing tree.
myTree = Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)
What I want to do is create a function maptree f which will act on Leaf. To be more specifically, f x = x +1
,
then maptree f myTree
will return
Node (Node (Leaf 2) (Leaf 3)) (Leaf 4)
My solution is
maptree f (Leaf a)= Leaf (f a)
maptree f (Node xl xr ) = Node (maptree xl) (maptree xr)
but it will return the following error
Couldn't match expected type `Tree a'
against inferred type `Tree t -> Tree t'
Probable cause: `maptree' is applied to too few arguments
In the first argument of `Node', namely `(maptree xl)'
In the expression: Node (maptree xl) (maptree xr)
Failed, modules loaded: none.
However, if I do
maptree (Leaf a)= Leaf ( a + 1)
maptree (Node xl xr ) = Node (maptree xl) (maptree xr)
it does work out.
I can not see the difference between the first function and the second one. How do I get error? Thanks.