I have to write a function in haskell that checks if two binary trees are mirror images of one another. This is what i have so far but i do not know if the True && areMirrorImages l1 r2 && areMirrorImages r1 l2 is the right way to do it. I was trying to logical AND True with the results of recursively calling the areMirrorImages functions.
-- Tests whether two BinaryTrees are mirror images of one another
areMirrorImages :: (Eq (BinaryTree a), Eq a) => BinaryTree a -> BinaryTree a -> Bool
areMirrorImages Empty Empty = True
areMirrorImages _ Empty = False
areMirrorImages Empty _ = False
areMirrorImages (Node x1 left1 right1) (Node x2 left2 right2) =
if (x1 == x2 && left1 == right2 && right1 == left2)
then True && (areMirrorImages left1 right2) && (areMirrorImages right1 left2)
else False
This is the error i get when i try to compile:
A2.hs:2:0:
Non-type variables, or repeated type variables,
in the constraint: Eq (BinaryTree a)
(Use -fglasgow-exts to permit this)
In the type signature:
areMirrorImages :: (Eq (BinaryTree a), Eq a) =>
BinaryTree a -> BinaryTree a -> Bool
I cant seem to figure out what's wrong
isMirrorOf x y = x == mirror y? Composing code like that tends to be more clear.True && expris unnecessary; it will always evaluate toexprso you can just drop theTrue &&.