Search code examples
listhaskellconstructorquadtree

Apply a function to a list and pass its result to a constructor?


How can just write flipvone time applying it to each element of list [se, sq, nw, ne], giving the result (not as a list of course) to the Q constructor?

data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a)
    deriving (Eq, Show)

flipv :: (Eq a, Show a) => QT a -> QT a
flipv (C a) = C a
flipv (Q nw ne se sw) = Q (flipv se) (flipv sw) (flipv nw) (flipv ne)

EDIT: note this actually wrong because the pointers should be: NW NE SW SE.


Solution

  • There is no particularly simple or compact method, but you could try this:

    flipv :: (Eq a, Show a) => QT a -> QT a
    flipv (C a) = C a
    flipv (Q nw ne se sw) = Q se' sw' nw' ne'
      where [nw', ne', se', sw'] = map flipv [nw, ne, se, sw]