1 {-# OPTIONS_GHC -Wall -fno-warn-orphans #-}
7 import qualified Data.List as L
10 -- glCons - a naive way to add an employee to a guest list
11 glCons :: Employee -> GuestList -> GuestList
12 glCons e@(Emp {empFun = funval}) (GL els fun) = GL (e:els) (fun + funval)
14 -- Monoid instance for GuestList
15 instance Monoid GuestList where
17 (GL el1 f1) `mappend` (GL el2 f2) = GL (el1 ++ el2) (f1 + f2)
20 moreFun :: GuestList -> GuestList -> GuestList
21 moreFun g1 g2 | g1 > g2 = g1
24 -- treeFold :: (Tree a -> b -> b) -> b -> Tree a -> b
25 -- treeFold combine e= treeDestructor e f
26 -- where f _ ts = let vs = map (\t1 -> treeFold combine e t1) ts in
27 -- foldr (\acc v -> combine v acc) e vs
28 -- treeDestructor _ f1 t1 = case t1 of
29 -- (Node v xs) -> f1 v xs
32 -- treeFold :: (t1 -> [b] -> b) -> Tree t1 -> b
33 -- treeFold combine = treeDestructor f
34 -- where f v ts = let vs = map (\t -> treeFold combine t) ts in
36 -- treeDestructor f1 t1 = case t1 of
37 -- (Node v xs) -> f1 v xs
38 treeFold :: (b -> a -> b) -> b -> Tree a -> b
39 treeFold f e (Node v []) = f e v
40 treeFold f e (Node v ts) = let b = foldr (\t acc -> treeFold f acc t) e ts
45 -- if the big boss comes, no question, his reportee managers are not
46 -- coming. So, we just take the send list from each pair and concat them.
47 -- if the big boss is not coming, then we pick the list from the pair that
48 -- has most fun and concat them all.
49 nextLevel :: Employee -> [(GuestList, GuestList)] -> (GuestList, GuestList)
51 let withBoss = glCons e $ mconcat $ map snd lst
52 withoutBoss = mconcat $ map (uncurry moreFun) lst
54 (withBoss, withoutBoss)
58 maxFun :: Tree Employee -> GuestList
59 maxFun = uncurry moreFun . maxFunPair
61 maxFunPair :: Tree Employee -> (GuestList, GuestList)
62 maxFunPair (Node e es) = nextLevel e $ map maxFunPair es
67 sortedGL :: GuestList -> (Integer, [String])
68 sortedGL (GL empls funval) = (funval, L.sort $ map empName empls)
70 getEmployeeList :: String -> String
71 getEmployeeList empls = let (f, ls) = sortedGL (maxFun (read empls))
77 -- empls <- readFile "company.txt"
78 emplList <- fmap getEmployeeList (readFile "company.txt")