How to do transform a tree using Scrap Your Boilerplate?
haskell, traversal, tree
Solution
You'll need to start with an SYB tutorial,
- http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB
The main traversal functions are:
- http://www.haskell.org/ghc/dist/stable/docs/libraries/syb/Data-Generics-Schemes.html#v%3Aeverywhere
- http://www.haskell.org/ghc/dist/stable/docs/libraries/syb/Data-Generics-Schemes.html#v%3Asomewhere
Play around with those to get a sense for the API, and you'll work it out.
SYB generics is a bit more than a beginner Haskell exercise though.
Problem
I am new to Haskell, so I am trying to figure out how to do tree traversals. Here is the Company example (with a slight change) that I have seen in several papers ``` data Company = C [Dept] deriving (Eq, Show, Typeable, Data) data Dept = D Name Manager [Unit] deriving (Eq, Show, Typeable, Data) data ThinkTank= TK Name [Unit] deriving (Eq, Show, Typeable, Data) data Unit = PU Employee | DU Dept deriving (Eq, Show, Typeable, Data) data Employee = E Person Salary deriving (Eq, Show, Typeable, Data) data Person = P Name Address deriving (Eq, Show, Typeable, Data) data Salary = S Float deriving (Eq, Show, Typeable, Data) type Manager = Employee type Name = String type Address = String ``` What I would like to do is move a Employee from where he is to a particular department. This person could be in a Department or a ThinkTank. It seems easy to do things in SYB as long as you are doing one type, but I am not sure how to deal with multiple data types.