summaryrefslogtreecommitdiff
path: root/lec08/Party.hs
blob: 05b5e0eec647dfdc9616a0221d9ca65bd3197d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Party where

import Employee
import Data.Tree

-- Exercise 1

glCons :: Employee -> GuestList -> GuestList
glCons e@Emp { empFun = ef } (GL es cf) = GL (e:es) (cf + ef)

-- mappend (a.k.a. (<>)) has been moved to Semigroup
instance Semigroup GuestList where
  (<>) (GL es1 f1) (GL es2 f2) = GL (es1 ++ es2) (f1 + f2)

instance Monoid GuestList where
  mempty = GL [] 0

moreFun :: GuestList -> GuestList -> GuestList
moreFun gl1@(GL _ f1) gl2@(GL _ f2)
  | f1 >= f2  = gl1
  | otherwise = gl2

-- Exercise 2

treeFold :: (a -> [b] -> b) -> Tree a -> b
treeFold f (Node l subs) = f l $ map (\t -> treeFold f t) subs