Reputation: 181
I've recently started getting into functional programming and have just started to learn Haskell. I am attempting to invert a binary tree and I have the following so far:
data Tree a = Tip | Bin (Tree a) a (Tree a) deriving (Show,Eq)
mirror :: Tree a -> Tree a
mirror(Bin l v r) = (mirror r) Bin v (mirror l)
However, I don't exactly understand why I get the following error: Couldn't match expected type ‘(Tree a0 -> a0 -> Tree a0 -> Tree a0)
. Can anyone explain where I am getting confused?
Upvotes: 1
Views: 567
Reputation: 477607
You need to put the data constructor first here, so it should be:
mirror :: Tree a -> Tree a
mirror(Bin l v r) = Bin (mirror r) v (mirror l)
By using (mirror r) Bin v mirror l
, you call Bin
as a parameter on the result of mirror r
, so ((mirror r) Bin)
. But that does not make much sense.
Note that you will need to add a case for Tip
as well, since right now, your function does not cover all possible values.
Upvotes: 4