-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek4_Notes.html
More file actions
395 lines (314 loc) · 19 KB
/
Week4_Notes.html
File metadata and controls
395 lines (314 loc) · 19 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
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
<!DOCTYPE html><html><head><meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>Week4_Notes</title></head><body><article class="markdown-body"><h1>
<a id="user-content-week4-notes-of-when-programs-get-bigger----functional-programming-in-haskell-mooc" class="anchor" href="#week4-notes-of-when-programs-get-bigger----functional-programming-in-haskell-mooc" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Week4 Notes of "When Programs Get Bigger" -- "Functional Programming in Haskell" MOOC</h1>
<h2>
<a id="user-content-contents" class="anchor" href="#contents" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Contents</h2>
<ul>
<li>
<a href="#program-structure">Program Structure</a>
<ul>
<li><a href="#let-expression"><code>let</code> expression</a></li>
<li><a href="#where-clause"><code>where</code> clause</a></li>
<li><a href="#let-vs-where"><code>let</code> vs <code>where</code></a></li>
<li><a href="#guards">Guards</a></li>
<li><a href="#case-expressions"><code>case</code> expressions</a></li>
<li><a href="#dealing-with-uncertainty">Dealing with uncertainty</a></li>
</ul>
</li>
<li>
<a href="#parsing-text">Parsing Text</a>
<ul>
<li><a href="#functional-machinery">Functional machinery</a></li>
<li>
<a href="#parsing-text">Parsing text</a>
<ul>
<li><a href="#alternative-approaches-to-parsing">Alternative approaches to parsing</a></li>
<li><a href="#parser-combinators">Parser combinators</a></li>
</ul>
</li>
<li>
<a href="#quick-primer-on-monads">Quick Primer on Monads</a>
<ul>
<li><a href="#key-syntactic-features-of-a-monad">Key syntactic features of a monad</a></li>
</ul>
</li>
<li><a href="#parsing-using-parsec">Parsing using Parsec</a></li>
</ul>
</li>
<li><a href="#quickcheck">QuickCheck</a></li>
</ul>
<hr>
<h2>
<a id="user-content-program-structure" class="anchor" href="#program-structure" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Program Structure</h2>
<h3>
<a id="user-content-let-expression" class="anchor" href="#let-expression" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><code>let</code> expression</h3>
<ul>
<li>A <code>let</code> expression provides local scope.</li>
<li>A <code>let</code> expression has a series of equations defining variable values and a final expression (after the <code>in</code> keyword) that computes a value with those variables in scope.</li>
<li>The variable names in a <code>let</code> expression should be lined up underneath one another.
<ul>
<li>Whitespace is important to interpret the code correctly.</li>
</ul>
</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-en">journeycost</span> <span class="pl-k">::</span> <span class="pl-en"><span class="pl-c1">Float</span></span> <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Float</span></span> <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Float</span></span>
journeycost miles fuelcostperlitre =
<span class="pl-k">let</span> milespergallon = <span class="pl-c1">35</span>
litrespergallon = <span class="pl-c1">4.55</span>
gallons = miles/milespergallon
<span class="pl-k">in</span> (gallons*litrespergallon*fuelcostperlitre)</pre></div>
<h3>
<a id="user-content-where-clause" class="anchor" href="#where-clause" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><code>where</code> clause</h3>
<ul>
<li>Inside an equation, <code>where</code> keyword provides definitions for variables that are used in the equation.</li>
<li>Similar to <code>let</code>, <code>where</code> must be indented more than the start of the enclosing equation.</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-en">cel2fahr</span> <span class="pl-k">::</span> <span class="pl-en"><span class="pl-c1">Float</span></span> <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Float</span></span>
cel2fahr x = (x*scalingfactor) + freezingpoint
<span class="pl-k">where</span> scalingfactor = <span class="pl-c1">9.0</span>/<span class="pl-c1">5.0</span>
freezingpoint = <span class="pl-c1">32</span></pre></div>
<h3>
<a id="user-content-let-vs-where" class="anchor" href="#let-vs-where" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><code>let</code> vs <code>where</code>
</h3>
<ul>
<li>
<p><code>let</code> and <code>where</code> have similarities:</p>
<ul>
<li>Both introduce a local scope.</li>
<li>Both allow any number of equations to be written.</li>
<li>Both allow the equations to be written in any order, and variables defined in any equation can be used ("are in scope") in the other equations.</li>
</ul>
</li>
<li>
<p>Yet there are a few differences:</p>
<ul>
<li>
<code>let</code> expressions are expressions;
<ul>
<li>
<code>let</code> can be used anywhere an expression is allowed.</li>
</ul>
</li>
<li>
<code>where</code> clauses are not expressions;
<ul>
<li>
<code>where</code> can be used only to provide some local variables for a top level equation.</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3>
<a id="user-content-guards" class="anchor" href="#guards" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Guards</h3>
<ul>
<li>Guards are a notation for defining functions based on predicate values.</li>
<li>Guards are easier to read than <code>if</code> / <code>then</code> / <code>else</code>, if there are more than two conditional outcomes</li>
<li>
<code>otherwise</code> guard should always be last, it’s like the <code>default</code> case in a C-style <code>switch</code> statement.</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-c">-- `absolute` method</span>
absolute x = <span class="pl-k">if</span> (x < <span class="pl-c1">0</span>) <span class="pl-k">then</span> (-x) <span class="pl-k">else</span> x
<span class="pl-c">-- same method using guards</span>
absolute x
| x < <span class="pl-c1">0</span> = -x
| <span class="pl-c1">otherwise</span> = x</pre></div>
<h3>
<a id="user-content-case-expressions" class="anchor" href="#case-expressions" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><code>case</code> expressions</h3>
<ul>
<li> A value with an algebraic data type may have one of several different forms — such as a <code>Leaf</code> or a <code>Node</code>, in the case of <code>Tree</code> structures.
<ul>
<li>Therefore to process such a value we need several segments of code, one for each possible form.</li>
</ul>
</li>
<li>The <code>case</code> expression examines the value, and chooses the corresponding clause.</li>
<li>Similar to a guard, but <code>case</code> expression does <strong><em>pattern matching</em></strong> and selects clause based on the value.
<ul>
<li>Like <code>otherwise</code> in guards, a <code>case</code> expression has a catch-all pattern: underscore character <code>_</code>, which means 'don't care' or 'match anything'.</li>
</ul>
</li>
<li>
<code>if</code> expression is just <em>syntactic sugar</em> for <code>case</code> expressions.</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-k">data</span> <span class="pl-en">Pet</span> <span class="pl-k">=</span> <span class="pl-ent">Cat</span> | <span class="pl-ent">Dog</span> | <span class="pl-ent">Fish</span>
<span class="pl-en">hello</span> <span class="pl-k">::</span> <span class="pl-en">Pet</span> <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">String</span></span>
hello x =
<span class="pl-k">case</span> x <span class="pl-k">of</span>
<span class="pl-ent">Cat</span> -> <span class="pl-s"><span class="pl-pds">"</span>meeow<span class="pl-pds">"</span></span>
<span class="pl-ent">Dog</span> -> <span class="pl-s"><span class="pl-pds">"</span>woof<span class="pl-pds">"</span></span>
<span class="pl-ent">Fish</span> -> <span class="pl-s"><span class="pl-pds">"</span>bubble<span class="pl-pds">"</span></span>
hello <span class="pl-ent">Dog</span> <span class="pl-c">-- > "woof"</span></pre></div>
<h3>
<a id="user-content-dealing-with-uncertainty" class="anchor" href="#dealing-with-uncertainty" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Dealing with uncertainty</h3>
<ul>
<li>
<code>Maybe</code> encapsulates an optional or possibly missing values.</li>
<li>
<code>Maybe</code> values denote missing results with <code>Nothing</code>.
<ul>
<li>This is type-safe and better than <code>null</code> in C-like languages.</li>
<li>
<code>Maybe</code> is like <code>Option</code> in Scala.</li>
</ul>
</li>
<li>
<code>Maybe</code> is either <code>Nothing</code> or <code>Just</code> a.</li>
<li>
<code>Maybe</code> derives two type classes:
<ul>
<li>
<code>Eq</code> for equality and</li>
<li>
<code>Ord</code> for comparison.</li>
</ul>
</li>
<li>
<code>Maybe</code> values can be propagated by:
<ul>
<li>Using lots of <code>case</code> statements or</li>
<li><code>Monads</code></li>
</ul>
</li>
<li>
<code>fmap</code>, a higher-order function applies function to the value inside <code>Maybe</code>.</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-en">maxhelper</span> <span class="pl-k">::</span> <span class="pl-en"><span class="pl-c1">Int</span></span> <span class="pl-k">-></span> [<span class="pl-en"><span class="pl-c1">Int</span></span>] <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Int</span></span>
maxhelper x <span class="pl-c1">[]</span> = x
maxhelper x (y:ys) = maxhelper (<span class="pl-k">if</span> x > y <span class="pl-k">then</span> x <span class="pl-k">else</span> y) ys
<span class="pl-c">-- get maximum item of a list</span>
<span class="pl-en">maxfromlist</span> <span class="pl-k">::</span> [<span class="pl-en"><span class="pl-c1">Int</span></span>] <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Maybe</span></span> <span class="pl-en"><span class="pl-c1">Int</span></span>
maxfromlist <span class="pl-c1">[]</span> = <span class="pl-ent"><span class="pl-c1">Nothing</span></span>
maxfromlist (x:xs) = <span class="pl-ent"><span class="pl-c1">Just</span></span> (maxhelper x xs)
maxfromlist [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>] <span class="pl-c">-- > Just 3</span>
maxfromlist <span class="pl-c1">[]</span> <span class="pl-c">-- > Nothing</span>
<span class="pl-c">-- how to apply a function to the value inside `Maybe`</span>
<span class="pl-k">let</span> inc = (+<span class="pl-c1">1</span>)
inc <span class="pl-c1">1</span> <span class="pl-c">-- > 2</span>
inc (<span class="pl-ent"><span class="pl-c1">Just</span></span> <span class="pl-c1">1</span>) <span class="pl-c">-- > Fails</span>
<span class="pl-c1">fmap</span> inc (<span class="pl-ent"><span class="pl-c1">Just</span></span> <span class="pl-c1">1</span>) <span class="pl-c">-- > Just 2</span>
<span class="pl-c1">fmap</span> inc <span class="pl-ent"><span class="pl-c1">Nothing</span></span> <span class="pl-c">-- > Nothing</span></pre></div>
<hr>
<h2>
<a id="user-content-parsing-text" class="anchor" href="#parsing-text" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Parsing Text</h2>
<h3>
<a id="user-content-functional-machinery" class="anchor" href="#functional-machinery" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Functional machinery</h3>
<ul>
<li>Returning functions as values
<ul>
<li>Functions that take functions as arguments.</li>
<li>Functions can also return functions as values.</li>
<li>2 different interpretations:</li>
</ul>
</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-c">-- `sum` is the result returned by partial application of (`foldl`).</span>
<span class="pl-c1">sum</span> = <span class="pl-c1">foldl</span> (+) <span class="pl-c1">0</span>
<span class="pl-c">-- `sum` is a function resulting from partial application of (`foldl`).</span>
<span class="pl-c1">sum</span> = \xs -> <span class="pl-c1">foldl</span> (+) <span class="pl-c1">0</span> xs</pre></div>
<ul>
<li>Function generators
<ul>
<li>We can use this concept to generate parameterised functions:</li>
</ul>
</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-c">-- Generates functions that add a constant number to their argument</span>
gen_add_n = \n ->
\x -> x+n
add_3 = gen_add_n <span class="pl-c1">3</span>
add_7 = gen_add_n <span class="pl-c1">7</span>
add_3 <span class="pl-c1">5</span> <span class="pl-c">--> 8</span>
add_7 <span class="pl-c1">4</span> <span class="pl-c">--> 11</span></pre></div>
<pre><code>* It is not limited to numeric constants:
</code></pre>
<div class="highlight highlight-source-haskell"><pre><span class="pl-c">-- Generates functions that perform a given arithmetic operation on a constant number and their argument</span>
gen_op_n = \op n ->
\x -> x <span class="pl-k">`op`</span> n
add_3 = gen_op_n (+) <span class="pl-c1">3</span>
mult_7 = gen_op_n (*) <span class="pl-c1">7</span>
add_3 <span class="pl-c1">5</span> <span class="pl-c">--> 8</span>
mult_7 <span class="pl-c1">4</span> <span class="pl-c">--> 28</span></pre></div>
<h3>
<a id="user-content-parsing-text-1" class="anchor" href="#parsing-text-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Parsing text</h3>
<ul>
<li>A functional program is organised around a tree-like data structure with an algebraic data type that represents the core data.</li>
<li>A parser reads text input and generates the tree.</li>
<li>Functions perform transformations or traversals on the tree.</li>
<li>Pretty-printer functions output the tree (original or transformed).</li>
</ul>
<h4>
<a id="user-content-alternative-approaches-to-parsing" class="anchor" href="#alternative-approaches-to-parsing" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Alternative approaches to parsing</h4>
<ul>
<li>User provides input in an awkward form. <em>Don't do it</em>.</li>
<li>Write the parser by hand, with just ordinary list processing functions. <em>Don't do it</em>.</li>
<li>Write the parser using regular expressions. <em>Don't do it</em>.</li>
<li>Use parser combinators. <em>Recommended approach</em>.</li>
<li>Use a parser generator; e.g. bison, antlr, happy. <em>Best approach for heavy-weight parsers</em>.</li>
</ul>
<h4>
<a id="user-content-parser-combinators" class="anchor" href="#parser-combinators" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Parser combinators</h4>
<ul>
<li>Parser combinators are functions that allow you to combine smaller parsers into bigger ones.</li>
<li>They are higher-order functions that take functions as arguments and return functions.</li>
<li>A parser combinator library provides both basic parsers (for words, numbers etc.) and combinators.</li>
</ul>
<h3>
<a id="user-content-quick-primer-on-monads" class="anchor" href="#quick-primer-on-monads" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Quick Primer on Monads</h3>
<ul>
<li>Monads are used to structure computations.</li>
<li>Example: <code>IO</code> monad is used to perform IO.</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-en">hello</span> <span class="pl-k">::</span> <span class="pl-en"><span class="pl-c1">String</span></span> <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">IO</span></span> <span class="pl-en"><span class="pl-c1">String</span></span>
hello x =
<span class="pl-k">do</span>
<span class="pl-c1">putStrLn</span> (<span class="pl-s"><span class="pl-pds">"</span>Hello, <span class="pl-pds">"</span></span> ++ x)
<span class="pl-c1">putStrLn</span> <span class="pl-s"><span class="pl-pds">"</span>What's your name?<span class="pl-pds">"</span></span>
name <- <span class="pl-c1">getLine</span>
<span class="pl-c1">return</span> name</pre></div>
<h4>
<a id="user-content-key-syntactic-features-of-a-monad" class="anchor" href="#key-syntactic-features-of-a-monad" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Key syntactic features of a monad</h4>
<ul>
<li>The <code>do</code> keyword;</li>
<li>The sequence of commands;</li>
<li>The way to extract information from a monadic computation using the left arrow <code><-</code>;</li>
<li>And the return keyword.
<ul>
<li>using the <code>do</code> notation is quite similar to imperative programming.</li>
</ul>
</li>
<li>A computation done in a monad returns a "monadic" type ==> string is returned inside the monad.
<ul>
<li>
<code>return</code> value of the above <code>hello</code> function: not just <code>String</code> but <code>IO String</code>.</li>
</ul>
</li>
</ul>
<h3>
<a id="user-content-parsing-using-parsec" class="anchor" href="#parsing-using-parsec" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Parsing using Parsec</h3>
<p><a href="https://github.com/wimvanderbauwhede/HaskellMOOC/tree/master/ParsecTutorial">Building a simple parser using the Parsec library</a></p>
<hr>
<h2>
<a id="user-content-quickcheck" class="anchor" href="#quickcheck" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>QuickCheck</h2>
<ul>
<li>QuickCheck automatically generates test cases for Haskell programs.</li>
<li>
<code>quickCheck</code> shows the results of the testcases executed.
<ul>
<li>
<code>verboseCheck</code> also shows the generated testcases.</li>
</ul>
</li>
</ul>
<div class="highlight highlight-source-haskell"><pre><span class="pl-c">-- Returns the absolute value of a number.</span>
absolute x
| x < <span class="pl-c1">0</span> = -x
| <span class="pl-c1">otherwise</span> = x
<span class="pl-c">-- Testcases can be generated using the following QuickCheck property specification.</span>
<span class="pl-k">import</span> <span class="pl-c1">Test.QuickCheck</span>
quickCheck ((\n -> (absolute (n) == n) || (<span class="pl-c1">0</span> - absolute(n) ==n)) <span class="pl-k">::</span> <span class="pl-en"><span class="pl-c1">Int</span></span> <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Bool</span></span>)
<span class="pl-c">-- Find minimum of a list [first element of a sorted list is its minimum].</span>
<span class="pl-k">import</span> <span class="pl-c1">Data.List</span>
<span class="pl-k">import</span> <span class="pl-c1">Test.VerboseCheck</span>
verboseCheck ((\l -> (<span class="pl-k">if</span> l == <span class="pl-c1">[]</span> <span class="pl-k">then</span> <span class="pl-ent"><span class="pl-c1">True</span></span> <span class="pl-k">else</span> (<span class="pl-c1">minimum</span> l) == (sort l) !! <span class="pl-c1">0</span>)) <span class="pl-k">::</span> [<span class="pl-en"><span class="pl-c1">Char</span></span>] <span class="pl-k">-></span> <span class="pl-en"><span class="pl-c1">Bool</span></span>)</pre></div>
</article></body></html>