-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch11_jammin.hs
More file actions
51 lines (39 loc) · 1.1 KB
/
ch11_jammin.hs
File metadata and controls
51 lines (39 loc) · 1.1 KB
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
48
49
50
51
module Jammin where
import Data.Bool
import Data.List
data Fruit =
Peach
| Plum
| Apple
| Blackberry
deriving (Eq, Show, Ord)
-- data JamJars =
-- Jam Fruit Int
-- deriving (Eq, Show)
data JamJars =
JamJars { fruit :: Fruit
, count :: Int }
deriving (Eq, Show, Ord)
allJam :: [JamJars]
allJam = [ JamJars Peach 3
, JamJars Apple 5
, JamJars Blackberry 3
, JamJars Blackberry 8
, JamJars Apple 3
, JamJars Peach 7 ]
jamCount :: [JamJars] -> Int
jamCount jars = sum $ map count jars
mostRow :: JamJars
mostRow = foldr
(\jam maxJam -> bool maxJam jam ((count jam) > (count maxJam)))
(head allJam)
allJam
compareJam :: JamJars -> JamJars -> Ordering
compareJam (JamJars _ x) (JamJars _ y) = compare x y
sortJam :: [JamJars]
sortJam = sortBy compareJam allJam
compareJamType :: JamJars -> JamJars -> Ordering
compareJamType (JamJars x _) (JamJars y _) = compare x y
sortJamType = sortBy compareJamType allJam
groupJam :: [[JamJars]]
groupJam = groupBy (\(JamJars x _) (JamJars y _) -> x == y) sortJamType