-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter-10.hs
189 lines (142 loc) · 3.75 KB
/
chapter-10.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
{-# LANGUAGE LambdaCase #-}
-- Chapter 10 exercises
-- For a later exercise
import Data.Time
-- Understanding Folds
-- 1 - b, c
-- 2
-- foldl (flip (*)) 1 [1..3]
-- foldl (flip (*)) (* 1 1) [2, 3]
-- foldl (flip (*)) (* 2 (* 1 1)) [3]
-- foldl (flip (*)) (* 3 (* 2 (* 1 1))) []
-- (* 3 (* 2 (* 1 1)))
-- 3 - c
-- 4 - a
-- 5
-- a
foldr (++) [] ["woot", "WOOT", "woot"]
-- b
foldr max [] "fear is the little death"
-- c
foldr (&&) True [False, True]
-- d -- No
-- e
foldl (flip ((++) . show)) "" [1..5]
-- f
foldr (flip const) 'a' [1..5]
-- g
foldr const '0' "t"
-- h
foldl (flip const) '0' "burritos"
-- i
foldl (flip const) 'z' ['1'..'5']
-- Exercises: Database Processing
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
, DbString "Hello, world!"
, DbDate (UTCTime
(fromGregorian 1921 5 1)
(secondsToDiffTime 34123))
]
-- 1
filterDbDate :: [DatabaseItem] -> [UTCTime]
filterDbDate = foldr (\item acc ->
case item of
DbDate utcTime -> utcTime : acc
_ -> acc)
[]
-- 2
filterDbNumber :: [DatabaseItem] -> [Integer]
filterDbNumber = foldr (\item acc ->
case item of
DbNumber num -> num : acc
_ -> acc)
[]
-- 3
mostRecent :: [DatabaseItem] -> UTCTime
mostRecent = maximum . filterDbDate
-- 4
sumDb :: [DatabaseItem] -> Integer
sumDb = sum . filterDbNumber
-- 5
avgDb :: [DatabaseItem] -> Double
avgDb xs = (fromIntegral . sumDb) xs / (fromIntegral . length . filterDbNumber) xs
-- Scans exercises
-- 1
fibs = take 20 $ 1 : scanl (+) 1 fibs
-- 2
fibs = takeWhile (< 100) $ 1 : scanl (+) 1 fibs
-- 3
fact = scanl (*) 1 [1..]
factN = (fact !!)
-- Chapter exercises
-- Warm-up and review
-- 1
stops = "pbtdkg"
vowels = "aeiou"
-- a
tuples = [(x, y, z) | x <- stops, y <- vowels, z <- stops]
-- b
tuples' = [('p', y, z) | y <- vowels, z <- stops]
-- c
abaTuples as bs = [(a, b, a') | a <- as, b <- bs, a' <- as]
-- 2
-- The function returns average length of words in a string.
-- Type: String -> Int
-- 3
seekritFunc x =
(fromIntegral . sum . map length . words) x / (fromIntegral . length . words) x
-- Rewriting functions using folds
-- 1
myOr :: [Bool] -> Bool
myOr = foldr (||) False
-- 2
myAny :: (a -> Bool) -> [a] -> Bool
myAny f = foldr (\x acc -> f x || acc) False
-- 3
myElem :: Eq a => a -> [a] -> Bool
myElem e = foldr (\x acc -> e == x || acc) False
myElem' :: Eq a => a -> [a] -> Bool
myElem' e = myAny (e ==)
-- 4
myReverse :: [a] -> [a]
myReverse = foldl (flip (:)) []
-- 5
myMap :: (a -> b) -> [a] -> [b]
myMap f = foldr (\x acc -> f x : acc) []
-- 6
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter f = foldr (\x acc -> if f x then x : acc else acc) []
-- 7
squish :: [[a]] -> [a]
squish = foldr (++) []
-- 8
squishMap :: (a -> [b]) -> [a] -> [b]
squishMap f = squish . myMap f
-- 9
squishAgain :: [[a]] -> [a]
squishAgain = squishMap id
-- 10
myMaximumBy :: (a -> a -> Ordering) -> [a] -> a
myMaximumBy f xs = foldr (\x acc ->
if f x acc == GT
then x
else acc)
(last xs)
(init xs)
-- 11
myMinimumBy :: (a -> a -> Ordering) -> [a] -> a
myMinimumBy f xs = foldr (\x acc ->
if f x acc == LT
then x
else acc)
(last xs)
(init xs)