-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter6Ex.hs
399 lines (279 loc) · 6.95 KB
/
Chapter6Ex.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
module Chapter6Ex where
import Prelude hiding (and, concat, drop, elem, init, last, length,
replicate, sum, take)
{-
1. Define the exponentiation operator ^ for non-negative integers using the
same pattern of recursion as the multiplication operator ∗, and show how
2 ^ 3 is evaluated using your definition.
-}
(<^>) :: Int -> Int -> Int
n <^> 0 = 1
n <^> 1 = n
n <^> e | n > 1 && e > 1 = n * (n <^> (e - 1))
| otherwise = error "negative num"
{-
2. Using the definitions given in this chapter, show how length [1, 2, 3],
drop 3 [1, 2, 3, 4, 5], and init [1, 2, 3] are evaluated.
-}
length :: [a] -> Int
length [] = 0
length (_ : xs) = 1 + length xs
{-
length [1, 2, 3]
1 + length [2, 3]
1 + (1 + length [3])
1 + (1 + (1 + length []))
1 + (1 + (1 + 0))
1 + (1 + 1)
1 + 2
3
-}
drop :: Int -> [a] -> [a]
drop 0 xs = xs
drop _ [] = []
drop n (x : xs) | n > 0 = drop (n - 1) xs
| otherwise = []
{-
drop 3 [1, 2, 3, 4, 5]
drop (3 - 1) [2, 3, 4, 5]
drop 2 [2, 3, 4, 5]
drop (2 - 1) [3, 4, 5]
drop 1 [3, 4, 5]
drop (1 - 1) [4, 5]
drop 0 [4, 5]
[4, 5]
-}
init :: [a] -> [a]
init [] = error "empty list"
init [x] = []
init (x : xs) = x : init xs
{-
init [1, 2, 3]
1 : init [2, 3]
1 : (2 : init [3])
1 : (2 : [])
1 : [2]
[1, 2]
-}
{-
3. Without looking at the definitions from the standard prelude, define the
following library functions using recursion.
– Decide if all logical values in a list are True :
and :: [Bool] -> Bool
-}
and :: [Bool] -> Bool
and [] = True
and (x : xs) = x && and xs
{-
and [True, False, True]
True && and [False, True]
True && (False && and [True])
True && (False && (True && and []))
True && (False && (True && True))
True && (False && True)
True && False
False
-}
{-
– Concatenate a list of lists:
concat :: [[a]] -> [a]
-}
concat :: [[a]] -> [a]
concat [] = []
concat (x : xs) = x ++ concat xs
{-
concat [[1, 2], [3, 4, 5], [6]]
[1, 2] ++ concat [[3, 4, 5], [6]]
[1, 2] ++ ([3, 4, 5] ++ concat [[6]])
[1, 2] ++ ([3, 4, 5] ++ ([6] ++ concat []))
[1, 2] ++ ([3, 4, 5] ++ ([6] ++ []))
[1, 2] ++ ([3, 4, 5] ++ [6])
[1, 2] ++ [3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
-}
{-
– Produce a list with n identical elements:
replicate :: Int -> a -> [a]
-}
replicate :: Int -> a -> [a]
replicate 0 _ = []
replicate 1 x = [x]
replicate n x | n > 0 = x : replicate (n - 1) x
| otherwise = []
{-
replicate 3 5
5 : replicate (3 - 1) 5
5 : replicate 2 5
5 : (5 : replicate (2 - 1) 5)
5 : (5 : replicate 1 5)
5 : (5 : [5])
5 : [5, 5]
[5, 5, 5]
-}
{-
– Select the nth element of a list:
(!!) :: [a] -> Int -> a
-}
nth :: [a] -> Int -> a
[] `nth` _ = error "empty list"
(x : xs) `nth` n | n == 0 = x
| n > 0 = xs `nth` (n - 1)
| otherwise = error "negative num"
{-
[1, 2, 3, 4, 5] `nth` 3
[2, 3, 4, 5] `nth` (3 - 1)
[2, 3, 4, 5] `nth` 2
[3, 4, 5] `nth` (2 - 1)
[3, 4, 5] `nth` 1
[4, 5] `nth` (1 - 1)
[4, 5] `nth` 0
4
-}
{-
– Decide if a value is an element of a list:
elem :: Eq a => a -> [a] -> Bool
-}
elem :: Eq a => a -> [a] -> Bool
elem _ [] = False
elem n (x : xs) | n == x = True
| otherwise = elem n xs
{-
elem 3 [1, 2, 3, 4, 5]
elem 3 [2, 3, 4, 5]
elem 3 [3, 4, 5]
True
-}
{-
Note: most of these functions are in fact defined in the prelude using other
library functions, rather than using explicit recursion.
-}
{-
4. Define a recursive function merge :: Ord a => [a] -> [a] -> [a] that
merges two sorted lists to give a single sorted list. For example:
> merge [2, 5, 6] [1, 3, 4]
[1, 2, 3, 4, 5, 6]
Note: your definition should not use other functions on sorted lists such as
insert or isort , but should be defined using explicit recursion.
-}
merge :: Ord a => [a] -> [a] -> [a]
merge [] [] = []
merge [] ys = ys
merge xs [] = xs
merge list1@(x: xs) list2@(y : ys) | x <= y = x : merge xs list2
| otherwise = y : merge list1 ys
{-
merge [2, 5, 6] [1, 3, 4]
1 : merge [2, 5, 6] [3, 4]
1 : (2 : merge [5, 6] [3, 4])
1 : (2 : (3 : merge [5, 6] [4]))
1 : (2 : (3 : (4 : merge [5, 6] [])))
1 : (2 : (3 : (4 : [5, 6])))
1 : (2 : (3 : [4, 5, 6]))
1 : (2 : [3, 4, 5, 6])
1 : [2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
-}
{-
5. Using merge , define a recursive function msort :: Ord a => [a] -> [a] that
implements merge sort, in which the empty list and singleton lists are already
sorted, and any other list is sorted by merging together the two lists that
result from sorting the two halves of the list separately.
Hint: first define a function halve :: [a] -> ([a], [a]) that splits a list into
two halves whose lengths differ by at most one.
-}
halve :: [a] -> ([a], [a])
halve xs = splitAt (length xs `div` 2) xs
msort :: Ord a => [a] -> [a]
msort [] = []
msort [x] = [x]
msort xs = merge (msort f) (msort s)
where (f, s) = halve xs
{-
msort [2, 5, 6, 1, 3, 4]
merge [2, 5, 6] [1, 3, 4]
....same as above....
-}
{-
6. Using the five-step process, define the library functions that calculate the
sum of a list of numbers, take a given number of elements from the start of
a list, and select the last element of a non-empty list.
-}
{-
sum
1. define the type
sum :: [Int] -> Int
2. enumerate the cases
sum []
sum [x]
sum [x, y]
sum (x : xs)
3. define the simple cases
sum [] = 0
sum [x] = x
sum [x, y] = x + y
sum (x : xs) = ...
4. define other cases
sum [] = 0
sum [x] = x
sum [x, y] = x + y
sum (x : xs) = x + sum xs
5. generalize and simplify
sum [] = 0
sum (x : xs) = x + sum xs
-}
sum :: [Int] -> Int
sum [] = 0
sum (x : xs) = x + sum xs
{-
take
1. define the type
take :: Int -> [a] -> [a]
2. enumerate the cases
take n []
take 0 xs
take n (x : xs)
3. define simple cases
take n [] = []
take 0 xs = []
take n (x : xs) = ...
4. define other cases
take n [] = []
take 0 xs = []
take n (x : xs) | n > 0 = x : take (n - 1) xs
| otherwise = []
5. generalize and simplify
take 0 _ = []
take _ [] = []
take n (x : xs) | n > 0 = x : take (n - 1) xs
| otherwise = []
-}
take :: Int -> [a] -> [a]
take 0 _ = []
take _ [] = []
take n (x : xs) | n > 0 = x : take (n - 1) xs
| otherwise = []
{-
last
1. define the type
last :: [a] -> a
2. enumerate the cases
last []
last [x]
last (x : xs)
3. define simple cases
last [] = error "empty"
last [x] = x
last (x : xs) = ....
4. define other cases
last [] = error "empty"
last [x] = x
last (x : xs) = last xs
5. generalize and simplify
last [] = error "empty"
last [x] = x
last (x : xs) = last xs
-}
last :: [a] -> a
last [] = error "empty"
last [x] = x
last (x : xs) = last xs