data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a)
deriving (Eq, Show)
Defining as described above, write a predicate to check if a given image (encoded as a quadrant) is symmetric about the vertical axis (horizontal symmetric). Use an anonymous function if possible.
Question . How would you implement a horizontal symmetry check for a given quadrant?
Well, I was thinking of something like this: when a square is just a leaf , in this case we have horizontal symmetry. The basic case is that quadtree has only one level (four sheets) of symmetry - it's just a color check (c1 == c2 && c3 == c4).
In any other case, I can check if this condition is satisfied: nw equals (fliphorizontal(ne)) && sw equals (fliphorizontal(se))where it fliphorizontalflips the quadrants horizontally, and equalschecks if the two quadrants are equal. However, I would like to avoid using an external function as much as possible, just anonymous if possible.
ishsymmetric :: (Eq a, Show a) => QT a -> Bool
ishsymmetric (C _) = True
ishsymmetric (Q (C c1) (C c2) (C c3) (C c4)) = c1 == c2 && c3 == c4
ishsymmetric (Q nw ne sw se) =
EDIT : fliph example:
fliph :: (Eq a, Show a) => QT a -> QT a
fliph (C a) = C a
fliph (Q nw ne sw se) = Q (fliph ne) (fliph nw) (fliph se) (fliph sw)
EDIT : The ultimate single-function solution (using the generic fold function for quadrants):
ishsymmetric :: (Eq a, Show a) => QT a -> Bool
ishsymmetric (C _) = True
ishsymmetric (Q a b c d) = and $ zipWith equals [a,c] [fliph b,fliph d]
where
fold f g (C c) = g c
fold f g (Q a b c d) = f (fold f g a) (fold f g b)
(fold f g c) (fold f g d)
fliph q = fold (\a b c d -> Q b a d c) (\c -> C c) q
equals (C c1) (C c2) = c1 == c2
equals (Q a b c d) (Q e f g h) = and $ zipWith equals [a,b,c,d] [e,f,g,h]