views:

151

answers:

2

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.

+3  A: 

You'll need to start with an SYB tutorial,

The main traversal functions are:

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.

Don Stewart
A: 

Not too familiar with SYB so I'll show an example using uniplate instead. The example only removes the employee unit from its dept or think tank but you'shll probly easily figure out how to extend it to do what you want.

> let jack = E (P "Jack" "The Road") (S 7)
> import Data.Generics.Uniplate.Data
> transformBi (filter (/= PU jack)) $ C [D "Operations" (E (P "Charles" "Seattle") (S 700000)) [PU jack]]
C [D "Operations" (E (P "Charles" "Seattle") (S 700000.0)) []]
yairchu