-
Notifications
You must be signed in to change notification settings - Fork 0
/
jammin.hs
47 lines (37 loc) · 1 KB
/
jammin.hs
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Jammin where
import Data.List
data Fruit =
Peach
| Plum
| Apple
| Blackberry
deriving (Eq, Show, Ord)
-- data JamJars =
-- Jam Fruit Int
-- deriving (Eq, Show)
data JamJars =
Jam { fruit :: Fruit
, jars :: Int}
deriving (Eq, Show, Ord)
row1 = Jam Blackberry 10
row2 = Jam Apple 20
row3 = Jam Peach 30
row4 = Jam Plum 40
row5 = Jam Blackberry 3
row6 = Jam Apple 10
allJam = [row1, row2, row3, row4, row5, row6]
totalJars :: [JamJars] -> Int
totalJars = foldr ((+) . jars) 0
mostRow :: [JamJars] -> JamJars
mostRow xs = foldr biggerJar (head xs) (tail xs)
where biggerJar a b =
case compare (jars a) (jars b) of
GT -> a
LT -> b
EQ -> a
compareKind (Jam k _) (Jam k' _) = compare k k'
sortJars :: [JamJars] -> [JamJars]
sortJars = sortBy compareKind
-- def took me a while to realize that sorting was required for groupBy to work properly
groupJam :: [JamJars] -> [[JamJars]]
groupJam = groupBy (\a b -> (compareKind a b) == EQ) . sortJars