]> git.rkrishnan.org Git - yorgey.git/blobdiff - hw8/Party.hs
bug fix in moreFun and solution to exercise 3
[yorgey.git] / hw8 / Party.hs
index 180e45a49c32b44fcf389722d0192633ccb27d24..d303b01f3296c99d6ea4933b020437cb0c5c2e42 100644 (file)
@@ -3,6 +3,7 @@ module Party where
 
 import Data.Monoid
 import Employee
+import Data.Tree
 
 -- exercise 1
 -- glCons - a naive way to add an employee to a guest list
@@ -16,5 +17,37 @@ instance Monoid GuestList where
 
 -- moreFun
 moreFun :: GuestList -> GuestList -> GuestList
-moreFun g1@(GL _ f1) g2@(GL _ f2) | f1 > f2 = g1
-                                  | otherwise = g2
+moreFun g1 g2 | g1 > g2 = g1
+              | otherwise = g2
+
+-- treeFold :: (Tree a -> b -> b) -> b -> Tree a -> b
+-- treeFold combine e= treeDestructor e f
+--     where f _ ts = let vs = map (\t1 -> treeFold combine e t1) ts in
+--                    foldr (\acc v -> combine v acc) e vs
+--           treeDestructor _ f1 t1 = case t1 of
+--                          (Node v xs) -> f1 v xs
+
+
+-- treeFold :: (t1 -> [b] -> b) -> Tree t1 -> b
+-- treeFold combine = treeDestructor f
+--      where f v ts = let vs = map (\t -> treeFold combine t) ts in
+--                     combine v vs
+--            treeDestructor f1 t1 = case t1 of
+--                                     (Node v xs) -> f1 v xs
+treeFold :: (b -> a -> b) -> b -> Tree a -> b
+treeFold f e (Node v []) = f e v
+treeFold f e (Node v ts) = let b = foldr (\t acc -> treeFold f acc t) e ts
+                           in
+                             f b v
+-- exercise 3
+-- nextLevel
+-- if the big boss comes, no question, his reportee managers are not
+-- coming. So, we just take the send list from each pair and concat them.
+-- if the big boss is not coming, then we pick the list from the pair that
+-- has most fun and concat them all.
+nextLevel :: Employee -> [(GuestList, GuestList)] -> (GuestList, GuestList)
+nextLevel e lst =
+    let withBoss = glCons e $ mconcat $ map snd lst
+        withoutBoss = mconcat $ map (uncurry moreFun) lst
+    in
+      (withBoss, withoutBoss)