-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlec06.html
606 lines (495 loc) · 19.2 KB
/
lec06.html
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Haskell - Lecture 6</title>
<meta name="description" content="A course on the world's fastest growing functional programming language">
<meta name="author" content="Nishant Shukla">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/serif.css" id="theme">
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<style>
body {
padding-top: 30px;
padding-bottom: 40px;
}
</style>
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/pojoaque.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
</head>
<body>
<script src="nav.js"></script>
<script>
function toggleElements(one, two) {
document.getElementById(one).style.display = 'none';
document.getElementById(two).style.display = 'block';
}
</script>
<div class="reveal">
<!-- Slides begin here! -->
<div class="slides">
<section>
<section>
<h1>Introduction To Haskell</h1>
<p>Lecture 6</p>
<p>
<br>
</p>
<p>Maps, Folds, and Beyond</p>
</section>
<section>
<h3>Using These Slides</h3>
<h4>Every slide has a secret note.</h4>
<small>
<ul>
<li>On <b>Chrome</b>: press <code>F12</code>, then click <b>Console</b></li>
<li>On <b>IE</b>: press <code>F12</code>, then click <b>Console</b></li>
<li>On <b>Firefox</b>: <code>Ctrl+Shift+k</code></li>
</ul>
</small>
<br>
<br>
<p><h3>Shortcut Keys:</h3></p>
<center>
<table width="80%">
<tr>
<td><code>↓</code>, <code>PgDn</code>, <code>n</code>, <code>j</code></td>
<td>next slide</td>
</tr>
<tr>
<td><code>↑</code>, <code>PgUp</code>, <code>p</code>, <code>k</code></td>
<td>prev slide</td>
</tr>
<tr>
<td><code>Esc</code></td>
<td>enables <code>ctrl+f</code> globally</td>
</tr>
</table>
</center>
<aside class="notes">Hi there! This is a secret lecture note. Every slide has a little blurb of text like this!</aside>
</section>
<script>
function clearAll() {
document.getElementById('treeA').style.display = 'none';
document.getElementById('treeB').style.display = 'none';
document.getElementById('treeC').style.display = 'none';
document.getElementById('treeD').style.display = 'none';
}
function showOne(tree) {
clearAll();
document.getElementById(tree).style.display = 'block';
}
</script>
<section>
<h3>Review of Homework 5</h3>
<small><a href="lec05.html#/0/23" target="_blank">Create a Binary Tree Data Type in Haskell</a></small>
<table style="width:100%">
<tr>
<td><center><a onclick="showOne('treeA');">A</a></center></td>
<td><center><a onclick="showOne('treeB');">B</a></center></td>
<td><center><a onclick="showOne('treeC');">C</a></center></td>
<td><center><a onclick="showOne('treeD');">D</a></center></td>
</tr>
</table>
<pre id='treeA'><code class='haskell'>
-- Designed by Matthew A. Frazier
-- "I haven't actually tested it, but it compiles so it should work"
data Tree a = Leaf a | Node a (Tree a) (Tree a)
deriving (Show, Eq)
add :: Integral a => Tree a -> a
add (Leaf l) = l
add (Node i right left) = i + (add right) + (add left)
</code></pre>
<pre id='treeB' style="display:none;"><code class='haskell'>
-- Designed by Rolph J. J. Recto
data Tree = Branch Int Tree Tree | Leaf Int | NullNode deriving (Show)
add :: Tree -> Int
add NullNode = 0
add (Leaf x) = x
add (Branch x left right) = x + (addTree left) + (addTree right)
</code></pre>
<pre id='treeC' style="display:none;"><code class='haskell'>
-- Mastered by James C. Sun
-- download: https://gist.github.com/BinRoot/4983189
module BinaryTree
(Tree(..), makeTree, add, printTree)
where
data Tree = Tree Int Tree Tree | EMPTYTREE deriving (Show, Eq)
printTree :: Tree -> IO()
printTree tree = putStrLn (printSubTree tree 0 0)
nodeLinks = ["", "/", "\\"]
printSubTree :: Tree -> Int -> Int -> String
printSubTree EMPTYTREE _ _ = ""
printSubTree (Tree root leftSubTree rightSubTree) n link = (printSubTree leftSubTree (n+3) 1)
++ (replicate n ' ')
++ (nodeLinks !! link)
++ (show root) ++ "\n"
++ (printSubTree rightSubTree (n+3) 2)
makeTree :: Int -> Tree
makeTree n
| n < 0 = EMPTYTREE
| otherwise = Tree n (makeTree (n - 1)) (makeTree (n - 2))
add :: Tree -> Int
add EMPTYTREE = 0
add (Tree root leftSubTree rightSubTree) = root + (add leftSubTree) + (add rightSubTree)
</code></pre>
<pre id='treeD' style="display:none;"><code class='haskell'>
-- Designed by Julien Letrouit
data Tree = Node {left::Tree, right::Tree, value::Int} | NilTree
treeSum NilTree = 0
treeSum node = value node + treeSum (left node) + treeSum (right node)
</code></pre>
<aside class="notes">As you can see, creating and traversing a tree in Haskell is very simple and elegant.</aside>
</section>
<section>
<h3>High Order Functions</h3>
<p>Functions can take functions as arguments</p>
<img src="L06_files/inception.png">
<aside class="notes">A function is a first class citizen of Haskell.</aside>
</section>
<section>
<h2>Maps</h2>
<p>A map applies a function to each element of a list</p>
<pre><code class="haskell">
Prelude> map even [1..10]
[False,True,False,True,False,True,False,True,False,True]
Prelude> map (+5) [1..10]
[6,7,8,9,10,11,12,13,14,15]
</code></pre>
<aside class="notes">The function `map` has two inputs: a function and a list. It applies the function to each element of the list to get the output.</aside>
</section>
<section>
<h3>Defining <code class="haskell">Map</code></h3>
<p class="fragment roll-in">Type signature:</p>
<pre class="fragment roll-in"><code class="haskell">
map :: (a -> b) -> [a] -> [b]
</code></pre>
<br>
<p class="fragment roll-in">Possible Implementation:</p>
<pre class="fragment roll-in"><code class="haskell">
map f [] = []
map f (x:xs) = (f x):(map f xs)
</code></pre>
<aside class="notes">It's exciting to see that implementing such a core functions is possible in one line of code.</aside>
</section>
<section>
<h3>Estimating π</h3>
<p><img src="L06_files/pi.png"></p>
<small>This summation is called the Gregory Series, and it converges to Pi</small>
<div class="fragment roll-in">
<pre><code>
piGuess :: Int -> Double
piGuess n = sum (map f [1..n])
f :: Int -> Double
f x = 4*(-1)^(x+1) / (2.0 * k - 1)
where k = fromIntegral x
</code></pre>
<small>Use a map! Bam!</small>
</div>
</section>
<section>
<h2>Filters</h2>
<p>A filter refines a list using a predicate</p>
<pre><code class="haskell">
Prelude> filter even [1..10]
[2,4,6,8,10]
Prelude> filter (>5) [1..10]
[6,7,8,9,10]
</code></pre>
<aside class="notes">A filter has two arguments just like a map: a function and a list. The output is a list of elements that pass the filter function.</aside>
</section>
<section>
<h3>Defining <code class="haskell">Filter</code></h3>
<p class="fragment roll-in">Type signature:</p>
<pre class="fragment roll-in"><code class="haskell">
filter :: (a -> Bool) -> [a] -> [a]
</code></pre>
<br>
<p class="fragment roll-in">Possible Implementation:</p>
<pre class="fragment roll-in"><code class="haskell">
filter p [] = []
filter p (x:xs) | p x = x : filter p xs
| otherwise = filter p xs
</code></pre>
<aside class="notes">This implementation uses both pattern matching and guards to form a readable definition of `filter`</aside>
</section>
<section>
<h2>Anonymous Function</h2>
<p>We can use λ-calculus to define a function</p>
<pre><code class="haskell">
Prelude> map (\x -> x*x) [1..10]
[1,4,9,16,25,36,49,64,81,100]
</code></pre>
<br>
<div class="fragment roll-in">
<p>This notation is inspired from <a target="_blank" href="http://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a></p>
<p>λx.(x*x)</p>
</div>
<aside class="notes">Lamda calculus allows us to define ad-hoc functions.</aside>
</section>
<section>
<h3>These Are Very Powerful</h3>
<small>In the code below, take a look at the λ-function</small>
<pre><code class="haskell">
Prelude> data Gender = Male | Female deriving (Show, Eq)
Prelude> let people = [(Male, "Mal"), (Female, "Zoe"),
(Male, "Wash"), (Female, "Inara"),
(Male, "Jayne"), (Female, "Kaylee")
(Male, "Simon"), (Female, "River")]
Prelude> filter (\(a,b) -> a==Female) people
[ (Female,"Zoe"), (Female,"Inara"),
(Female,"Kaylee"), (Female,"River") ]
Prelude> map snd it
["Zoe", "Inara", "Kaylee", "River"]
</code></pre>
<aside class="notes">List of tuples is actually a very common way to storing key-value pairs.</aside>
</section>
<section>
<h2>Folds</h2>
<p>A fold scans an entire list to return a result</p>
<pre><code class="haskell">
-- sum up all elements of a list
Prelude> foldl (+) 0 [1, 2, 3]
6
-- count the number of vowels in a String
Prelude> foldl (\acc x -> if x `elem` "aeiou"
then acc+1
else acc) 0 "hello world"
2
</code></pre>
<img src="L06_files/fold.jpg">
<aside class="notes">A fold takes a function, an accumulator value, and a list, and returns some transformation on the accumulator.</aside>
</section>
<section>
<h2>Scans</h2>
<p>A scan shows the intermediate values of a fold</p>
<pre><code class="haskell">
-- sum up all elements of a list
Prelude> scanl (+) 0 [1, 2, 3]
[0,1,3,6]
-- count the number of vowels in a String
Prelude> scanl (\acc x -> if x `elem` "aeiou"
then acc+1
else acc) 0 "hello world"
[0,0,1,1,1,2,2,2,3,3,3,3]
</code></pre>
<aside class="notes">A scan is basically the same thing as a fold, but it shows all the intermediate steps.</aside>
</section>
<section>
<h2>Function Application</h2>
<p>The <code class="haskell">($)</code> function is called a function application.</p>
<p>It makes functions right associative</p>
<pre class="fragment roll-in"><code class="haskell">
Prelude> not odd 4
ERROR!
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude> not (odd 4)
True
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude> not $ odd 4
True
</code></pre>
<aside class="notes">It's for code readability.</aside>
</section>
<section>
<h2>The <code class="haskell">.</code> Function</h2>
<p>It composes functions in a readable manner</p>
<br>
<p><code class="haskell">f(g(h(k(x))))</code> is ugly</p>
<p><code class="haskell">(f.g.h.k)(x)</code> is pretty</p>
<br>
<pre><code class="haskell">
Prelude> (not.odd) 4
True
Prelude> (length.head.words) "University of Virginia"
10
</code></pre>
<aside class="notes">As you can see, these abstract functions give you a log of power over how you prefer to write your Haskell code.</aside>
</section>
<section>
<h3>Plethora of Functions</h3>
<p>These are only <b>some</b> of the functions in <i>Prelude</i></p>
<br>
<p>Haskell comes with a bunch more:</p>
<ul>
<li>Data.List</li>
<li>Data.Char</li>
<li>Data.Map</li>
<li>Data.Set</li>
</ul>
<br>
<br>
<p>...and more than <a target="_blank" href="http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Prelude.html">350 others</a>!</p>
<aside class="notes">No need to memorize these functions. You will learn to use the right ones over time.</aside>
</section>
<section>
<h2>Data.List</h2>
<pre class="fragment roll-in"><code class="haskell">
Prelude> import Data.List
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.List> concat ["under","stand","able"]
"understandable"
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.List> any (==0) [1,1,1,1,1,0,1]
True
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.List> sort "hello"
"ehllo"
</code></pre>
<small class="fragment roll-in">...and over <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base//Data-List.html">200 more</a>!</small>
<aside class="notes">Data.List contains useful list manipulation functions.</aside>
</section>
<section>
<h2>Data.Char</h2>
<pre class="fragment roll-in"><code class="haskell">
Prelude> import Data.Char
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Char> isNumber 'h'
False
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Char> toUpper 't'
'T'
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Char> map ord ['A'..'F']
[65,66,67,68,69,70]
</code></pre>
<small class="fragment roll-in">...and over <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base//Data-Char.html">100 more</a>!</small>
<aside class="notes">Data.Char contains lots of useful character and string manipulation functions.</aside>
</section>
<section>
<h2>Data.Map</h2>
<pre class="fragment roll-in"><code class="haskell">
Prelude> import Data.Map
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Map> let m = fromList [("CS", "Computer Science"),
("PHIL", "Philosophy")
("ASTR", "Astronomy")]
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Map> keys m
["CS","PHIL","ASTR"]
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Map> Data.Map.lookup "CS" m
Just "Computer Science"
</code></pre>
<small class="fragment roll-in">...and over <a href="http://www.haskell.org/ghc/docs/6.12.2/html/libraries/containers-0.3.0.0/Data-Map.html">200 more</a>!</small>
<aside class="notes">Hashmaps can be used from the Data.Map package.</aside>
</section>
<section>
<h2>Data.Set</h2>
<pre class="fragment roll-in"><code class="haskell">
Prelude> import Data.Set
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Set> let a = fromList [1..58]
Prelude Data.Set> let b = fromList [53..100]
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Set> intersection a b
fromList [53,54,55,56,57,58]
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.Set> findMax $ union a b
100
</code></pre>
<small class="fragment roll-in">...and around <a href="http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Data-Set.html">100 more</a>!</small>
<aside class="notes"></aside>
</section>
<section>
<h1>Homework</h1>
<ol>
<li>Fill out this <a target="_blank" href="https://docs.google.com/forms/d/1WX__zBdNU6yPRMUtB9PmINrXLDms5IRq9izb0cZa8SM/viewform">form</a>!</li>
<li>Create a password strength checker</li>
<small>
<br>
<p>A strong password has</p>
<ul>
<li>at least 15 characters</li>
<li>uppercase letters</li>
<li>lowercase letters</li>
<li>numbers</li>
</ul>
</small>
</ol>
<pre><code class="haskell">
Prelude> :t strong
strong :: String -> Bool
Prelude> strong "sup3rL33Tpassw0rd"
True
</code></pre>
<small>Try to use tools you've learned this lecture</small>
</section>
<aside class="notes">Make a function for each of the 4 cases for a strong password. Create functions generously!</aside>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
mouseWheel: true,
rollingLinks: false,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.3.min.js"><\/script>')</script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/main.js"></script>
<script>
Reveal.addEventListener( 'slidechanged', function( event ) {
// event.previousSlide, event.currentSlide, event.indexh, event.indexv
var notes = event.currentSlide.querySelector(".notes");
if(notes) {
console.info("---");
console.info(notes.innerHTML.replace(/\n\s+/g,'\n'));
}
} );
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-29747714-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>