-
Notifications
You must be signed in to change notification settings - Fork 3
/
chapter-10.hs
358 lines (211 loc) · 6.29 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import Data.List
-- Exercise 10.1
doubleAllLC :: [Integer] -> [Integer]
doubleAllLC xs = [2 * x | x <- xs]
doubleAllRecur :: [Integer] -> [Integer]
doubleAllRecur [] = []
doubleAllRecur (x:xs) = 2 * x : doubleAllRecur xs
doubleAllMap :: [Integer] -> [Integer]
doubleAllMap xs = map (2*) xs
{-
doubleAllLC [2, 1, 7]
~ [2 * 2, 2 * 1, 2 * 7]
~ [4, 2, 14]
doubleAllRecur [2, 1, 7]
~ 2 * 2 : doubleAllRecur [1, 7]
~ 2 * 2 : 2 * 1 : doubleAllRecur [7]
~ 2 * 2 : 2 * 1 : 2 * 7 : doubleAllRecur []
~ 2 * 2 : 2 * 1 : 2 * 7 : []
~ [2 * 2, 2 * 1, 2 * 7]
~ [4, 2, 14]
doubleAllMap [2, 1, 7]
~ [ (2*) 1, (2*) 1, (2*) 7]
~ [4, 2, 14]
-}
-- Exercise 10.2
length' :: [a] -> Integer
length' xs = sum (map one xs)
where
one _ = 1
-- Exercise 10.3
addUp ns = filter greaterOne (map addOne ns)
where
greaterOne n = n > 1
addOne n = n + 1
addUp' ns = map addOne (filter greaterZero ns)
where
greaterZero n = n > 0
addOne n = n + 1
-- Exercise 10.4
someFn1 ns = map addOne (map addOne ns)
where
addOne n = n + 1
-- Adds 2 to each number in ns
-- Exercise 10.5
someFn2 ns = filter greaterOne (filter lessTen ns)
where
greaterOne n = n > 1
lessTen n = n < 10
{-
Only numbers between 1 and 10 but not inclusive, are filtered.
filter p (filter q xs) => filter (p and q) ns
-}
-- Exercise 10.6
squareThem :: [Integer] -> [Integer]
squareThem xs = map (^2) xs
sumOfSquares :: [Integer] -> Integer
sumOfSquares xs = sum (squareThem xs)
greaterThanZero :: [Integer] -> Bool
greaterThanZero xs = length xs == length (filter ( > 0 ) xs)
-- Exercise 10.7
type Func = Integer -> Integer
test_min_f :: Func -> Integer -> Integer
test_min_f f n = minimum (map f [0..n])
test_eq_f :: Func -> Integer -> Bool
test_eq_f f n = length (filter (== (f 0)) (map f [0..n])) == length [0..n]
test_gt0_f :: Func -> Integer -> Bool
test_gt0_f f n = length (filter (> 0) (map f [0..n])) == length [0..n]
test_increasing_f :: Func -> Integer -> Bool
test_increasing_f f n = (map f [0..n]) == sort (map f [0..n])
some_f :: Func
some_f x = 100 - x + 1
const_f :: Func
const_f _ = 3
inc_f :: Func
inc_f x = x + 1 -- always increasing
dec_f :: Func
dec_f x = 100 - x -- always increasing for x < 100
-- Exercise 10.8
twice :: (Integer -> Integer) -> Integer -> Integer
twice f n = f ( f n)
twiceGeneral :: (a -> a) -> a -> a
twiceGeneral f x = f (f x)
-- Exercise 10.9
iter :: Integer -> (a -> a) -> a -> a
iter 0 f x = x
iter n f x = f $ iter (n - 1) f x
-- Exercise 10.10
double :: Integer -> Integer
double x = 2 * x
twoN :: Integer -> Integer
twoN n = iter n double 1
-- Exercise 10.11
{-
Quickcheck for
filter p xs
prop_filter_1 p xs = length [1 | x <- xs, p x == False] == 0
-}
-- Exercise 10.12
{-
Quickcheck for
f (g x) ~ x
g (f y) ~ y
map f xs
prop_fg_1 f g xs = map g (map f xs) == map f (map g xs)
-}
-- Exercise 10.13
sum_of_squares':: Integer -> Integer
sum_of_squares' n = foldr (+) 0 $ map (^2) [1..n]
-- Exercise 10.14
sum_sq_pos_int :: [Integer] -> Integer
sum_sq_pos_int xs = foldr (+) 0 [x*x | x <- xs, x > 0]
-- Exercise 10.15
unzip' :: [(a, b)] -> ([a], [b])
unzip' xs = (foldr first [] xs, foldr second [] xs)
where first z zs = [fst z] ++ zs
second z zs = [snd z] ++ zs
-- There should be a better way to do this.
-- What we're effectively doing is reversing the list then
-- taking the head.
last' :: Eq a => [a] -> a
last' xs = head $ foldr p [] xs
p :: Eq a => a -> [a] -> [a]
p x y
| y == [] = [x]
| otherwise = y
{-
init' :: [a] -> [a]
init' xs = tail $ foldr q [] xs
q :: a -> [a] -> [a]
q x y
| y == [] = [x]
| otherwise = y
-}
-- Exercise 10.16
mystery xs = foldr (++) [] (map sing xs)
where sing x = [x]
-- It just takes an array and returns an indentical array
-- Exercise 10.17
-- From Chapter 6:
type Name = String
type Price = Int
type LineItem = (Name, Price)
formatLine :: LineItem -> String
formatLine (name, price) = name ++ replicate n '.' ++ priceStr ++ "\n"
where n = 30 - (length name) - (length priceStr)
priceStr = formatPence price
formatPence :: Price -> String
formatPence n = (show pounds) ++ "." ++ (pad0 pence )
where pounds = n `div` 100
pence = n `mod` 100
pad0 p = if p < 10 then
"0" ++ show (p)
else
show p
formatLines :: [LineItem] -> String
formatLines lines = concat [formatLine line | line <- lines]
-- test data
shopping :: [LineItem]
shopping = [("Dry Sherry, 1Lt", 540),
("Wet Sherry ,2Lt", 1234),
("Pink Lemonade, 3Lt", 403)]
formatList :: (a -> String) -> [a] -> String
formatList f xs = foldr (++) [] (map f xs)
formatLines' :: [LineItem] -> String
formatLines' lines = formatList formatLine lines
-- Exercise 10.18
filterFirst :: (a -> Bool) -> [a] -> [a]
filterFirst p (x:xs)
| p x == False = xs
| otherwise = x : filterFirst p xs
{-
ghci> filterFirst isDigit "123"
"123*** Exception: chapter-10.hs:(256,1)-(258,42): Non-exhaustive patterns in function filterFirst
Add the line below to get it working.
-}
filterFirst p [] = []
-- Exercise 10.19
filterLast :: (a -> Bool) -> [a] -> [a]
filterLast p [] = []
filterLast p xs
| p (last xs) == False = init xs
| otherwise = (filterLast p (init xs)) ++ [last xs]
-- In terms of filterFirst
filterLast' :: (a -> Bool) -> [a] -> [a]
filterLast' p xs = reverse $ filterFirst p $ reverse xs
-- Exercise 10.20
switchMap :: (a -> b) -> (a -> b) -> [a] -> [b]
switchMap _ _ [] = []
switchMap f g (x:xs) = f x : switchMap g f xs
-- TODO: Re-implement using foldr?
-- Exercise 10.21
split :: [a] -> ([a], [a])
split xs = (foldr p [] xs, foldr q [] xs)
where p z zs = [z] ++ [head zs]
q y ys = [y] -- ++ [head (tail ys)]
-- Exercise 10.22
-- Exercise 10.23
-- Exercise 10.24
-- Exercise 10.25
-- Exercise 10.26
-- Exercise 10.27
-- Exercise 10.28
-- Exercise 10.29
-- Exercise 10.30
-- Exercise 10.31
-- Exercise 10.32
-- Exercise 10.33
-- Exercise 10.34
-- Exercise 10.35
-- Exercise 10.36
-- Exercise 10.37