-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter19.lhs
More file actions
230 lines (148 loc) · 4.23 KB
/
Copy pathChapter19.lhs
File metadata and controls
230 lines (148 loc) · 4.23 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
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
Haskell: The Craft of Functional Programming
Simon Thompson
(c) Addison-Wesley, 1999.
Chapter 19
Time and space behaviour
^^^^^^^^^^^^^^^^^^^^^^^^
> module Chapter19 where
> import Prelude hiding (map)
Various functions whose complexity is discussed.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Naive Fibonacci function
> fib 0 = 0
> fib 1 = 1
> fib m = fib (m-2) + fib (m-1)
Naive factorial function
> fac :: Int -> Int
> fac 0 = 1
> fac n = n * fac (n-1)
Insertion sort
> iSort :: Ord a => [a] -> [a]
> iSort [] = []
> iSort (x:xs) = ins x (iSort xs)
> ins :: Ord a => a -> [a] -> [a]
> ins x [] = [x]
> ins x (y:ys)
> | (x<=y) = x:y:ys
> | otherwise = y:ins x ys
Quicksort
> qSort :: Ord a => [a] -> [a]
> qSort [] = []
> qSort (x:xs) = qSort [z|z<-xs,z<=x] ++ [x] ++ qSort [z|z<-xs,z>x]
Two reverse functions
> rev1 [] = []
> rev1 (x:xs) = rev1 xs ++ [x]
> rev2 = shunt []
> shunt xs [] = xs
> shunt xs (y:ys) = shunt (y:xs) ys
Two multiplication functions
> mult n 0 = 0
> mult n m = mult n (m-1) + n
> russ n 0 = 0
> russ n m
> | (m `mod` 2 == 0) = russ (n+n) (m `div` 2)
> | otherwise = russ (n+n) (m `div` 2) + n
The merge sort function
> mSort xs
> | (len < 2) = xs
> | otherwise = mer (mSort (take m xs)) (mSort (drop m xs))
> where
> len = length xs
> m = len `div` 2
> mer (x:xs) (y:ys)
> | (x<=y) = x : mer xs (y:ys)
> | otherwise = y : mer (x:xs) ys
> mer (x:xs) [] = (x:xs)
> mer [] ys = ys
Implementations of sets
^^^^^^^^^^^^^^^^^^^^^^^
Sets implemented as _unordered_ lists.
type Set a = [a]
empty = []
memSet = member
inter xs ys = filter (member xs) ys
union = (++)
subSet xs ys = and (map (member ys) xs)
eqSet xs ys = subSet xs ys && subSet ys xs
makeSet = id
mapSet = map
Space behaviour
^^^^^^^^^^^^^^^
Lazy evaluation
^^^^^^^^^^^^^^^
List examples
> exam1 n = [1 .. n] ++ [1 .. n]
> exam2 n = list ++ list
> where
> list=[1 .. n]
> exam3 n = [1 .. n] ++ [last [1 .. n]]
> exam4 n = list ++ [last list]
> where
> list=[1 .. n]
Saving space?
^^^^^^^^^^^^^
A new version of factorial
> newFac :: Int -> Int
> newFac n = aFac n 1
> aFac 0 p = p
> aFac n p = aFac (n-1) (p*n)
This can be modified thus:
aFac n p
| p==p = aFac (n-1) (p*n)
Miscellaneous functions
> sumSquares :: Integer -> Integer
> sumSquares n = sumList (map sq [1 .. n])
> sumList = foldr (+) 0
> sq n = n*n
Folding revisited
^^^^^^^^^^^^^^^^^
Map defined using foldr
> map f = foldr ((:).f) []
Factorial using foldr
> facFold n = foldr (*) 1 [1 .. n]
Examples
> foldEx1 n = foldr (&&) True (map (==2) [2 .. n])
Avoiding re-computation: memoization
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The Fibonacci numbers
A naive algorithm is given earlier in this script.
An algorithm which returns a pair of consecutive Fibonacci numbers.
> fibP :: Int -> (Int,Int)
> fibP 0 = (0,1)
> fibP n = (y,x+y)
> where
> (x,y) = fibP (n-1)
The list of Fibonacci values, defined directly.
> fibs ::[Int]
> fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Dynamic programming: maximal common subsequence
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The naive algorithm ...
> mLen :: Eq a => [a] -> [a] -> Int
> mLen xs [] = 0
> mLen [] ys = 0
> mLen (x:xs) (y:ys)
> | x==y = 1 + mLen xs ys
> | otherwise = max (mLen xs (y:ys)) (mLen (x:xs) ys)
... translated to talk about sub-components of lists, described by their
endpoints ...
> maxLen :: Eq a => [a] -> [a] -> Int -> Int -> Int
> maxLen xs ys 0 j = 0
> maxLen xs ys i 0 = 0
> maxLen xs ys i j
> | xs!!(i-1) == ys!!(j-1) = (maxLen xs ys (i-1) (j-1)) + 1
> | otherwise = max (maxLen xs ys i (j-1))
> (maxLen xs ys (i-1) j)
... and then transliterated into a memoised version.
> maxTab :: Eq a => [a] -> [a] -> [[Int]]
> maxTab xs ys
> = result
> where
> result = [0,0 .. ] : zipWith f [0 .. ] result
> f i prev
> = ans
> where
> ans = 0 : zipWith g [0 .. ] ans
> g j v
> | xs!!i == ys!!j = prev!!j + 1
> | otherwise = max v (prev!!(j+1))