-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch10.hs
42 lines (34 loc) · 1.15 KB
/
ch10.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
module Ch10 where
import Data.Time
data DatabaseItem = DbString String
| DbNumber Integer
| DbDate UTCTime
deriving (Eq, Ord, Show)
theDatabase :: [DatabaseItem]
theDatabase =
[ DbDate (UTCTime
(fromGregorian 1911 5 1)
(secondsToDiffTime 34123))
, DbNumber 9001
, DbNumber 9002
, DbNumber 9002
, DbString "Hello World"
, DbDate (UTCTime
(fromGregorian 1921 5 1)
(secondsToDiffTime 34123))
]
filterDbDate :: [DatabaseItem] -> [UTCTime]
filterDbDate [] = []
filterDbDate (DbDate date:xs) = date : filterDbDate xs
filterDbDate (_:xs) = filterDbDate xs
filterDbNumber :: [DatabaseItem] -> [Integer]
filterDbNumber [] = []
filterDbNumber (DbNumber number:xs) = number : filterDbNumber xs
filterDbNumber (_:xs) = filterDbNumber xs
mostRecent :: [DatabaseItem] -> UTCTime
mostRecent db = foldr max (head dates) (tail dates)
where dates = filterDbDate $ db
sumDb :: [DatabaseItem] -> Integer
sumDb db = foldr (+) 0 $ filterDbNumber db
avgDb :: [DatabaseItem] -> Double
avgDb db = fromIntegral (sumDb db) / fromIntegral (length $ filterDbNumber db)