-
Notifications
You must be signed in to change notification settings - Fork 5
/
lec02.html
621 lines (531 loc) · 20.5 KB
/
lec02.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Haskell - Lecture 2</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'>
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/pojoaque.css">
<style>
body {
padding-top: 30px;
padding-bottom: 40px;
}
code {
margin-left:50%;
}
</style>
<!-- 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>
</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 2</p>
<p>
<br>
</p>
<p>([<font color='#aa4422'>"lists"</font>, <font color='#aa4422'>"tuples"</font>], <font color='#aa4422'>"and data types"</font>)</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>
<!-- Do a live example with `mod` -->
<section>
<h3>Jumping into GHCi</h3>
<small>Make Haskell your calculator for the next few months</small>
<pre class="fragment roll-in haskell"><code>
Prelude> 4317 * 103
444651
</code></pre>
<pre class="fragment roll-in haskell"><code>
Prelude> let x = 3.0
Prelude> let y = 4.0
Prelude> sqrt( x^2 + y^2 )
5.0
</code></pre>
<pre class="fragment roll-in haskell"><code>
Prelude> 2^1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
</code></pre>
<aside class="notes">GHC is a Haskell compiler, and GHCi is the interactive environment for writing Haskell code. We will use GHCi for most of the demos, and eventually we'll grow out of it and start writing our code in a text-file.</aside>
</section>
<!-- Do a live example with `mod` -->
<section>
<h2>Functions are Everywhere</h2>
<div>
<pre><code class="haskell">
Prelude> (+) 2 3
5
</pre></code>
<p><small>f(2, 3) = 2 + 3 = 5</small></p>
</div>
<div>
<pre><code class="haskell">
Prelude> (^) 3 4
81
</pre></code>
<p><small>g(3, 4) = 3^4 = 81</small></p>
</div>
<aside class="notes">Haskell allows you to infix some symbols for your convenience. You can write 100 `mod` 3 instead of mod 100 3. Also, unlike some other languages, Haskell statements do not end with a semi-colon.</aside>
</section>
<section data-state="soothe">
<h3>Bool</h3>
<pre class="fragment roll-in"><code class="haskell">
Prelude> (True && False) || False
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
False
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude> True && 1
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
ERROR!
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude> 30 < 31
True
</code></pre>
<aside class="notes">The only boolean data types are True and False. Unlike some other languages, the number 0 does not mean False, and the number 1 does not mean True.</aside>
</section>
<!--
This is how you define new functions in ghci.
Use existing code.
'not' keyword instead of '!'
Do live example of using /= instead of !=
-->
<section>
<h3>if ... then ... else ...</h3>
<p>Lets define a function <code>royal</code></p>
<pre><code class="haskell">
Prelude> let royal p = if p>9000 then True else False
Prelude> royal 9001
True
</code></pre>
<br>
<div class="fragment roll-in">
<p>Lets also define a related function <code>peasant</code></p>
<pre><code class="haskell">
Prelude> let peasant p = not (royal p)
Prelude> peasant 9001
False
</code></pre>
</div>
<p class="fragment roll-in">Reusing code is easy</p>
<aside class="notes">Even an if-statement is a function. 'not' is a function that takes in a Bool and negates it.</aside>
</section>
<section>
<h2>Lists</h2>
<p>The most common data type in Haskell</p>
<br>
<h1>[☺, ☹, ☺]</h1>
<br>
<ul>
<li>elements are comma-separated</li>
<li>surrounded by square brackets [ ... ]</li>
<li>an empty list is simply []</li>
</ul>
<aside class="notes">A list is a collections of things that have the same Type. You should always think, 'a list of what?' We can have a list of numbers, a list of Bools, and so on.</aside>
</section>
<section>
<h3>Try it out</h3>
<pre><code class="haskell">
Prelude> [3,1,5,3]
[3,1,5,3]
</code></pre>
<pre><code class="haskell">
Prelude> ["list","of","strings"]
["list","of","strings"]
</code></pre>
<br>
<p>The elements must be of the same type</p>
<pre><code class="haskell">
Prelude> [1,2,3,"a","bb","ccc"]
ERROR!
</code></pre>
<aside class="notes">A list is just a comma separated value.</aside>
</section>
<!-- Do example of [1..] again -->
<section>
<h3>Enumeration</h3>
<p>Start at 1, end at 10</p>
<pre><code class="haskell">
Prelude> [1..10]
[1,2,3,4,5,6,7,8,9,10]
</code></pre>
<br>
<p>Start at 1, count up by 0.25, end at 4</p>
<pre><code class="haskell">
Prelude> [1, 1.25 .. 4.0]
[1.0,1.25,1.5,1.75,2.0,2.25,2.5,2.75,3.0,3.25,3.5,3.75,4.0]
</code></pre>
<aside class="notes">The '..' notation reads our mind and creates a list with the elements interpolated.</aside>
</section>
<section>
<h3>List Operators</h3>
<h4>Concatenation (++)</h4>
<pre><code class="haskell">
Prelude> let l1 = [1,2,3]
Prelude> let l2 = [4,5,6]
Prelude> l1 ++ l2
[1,2,3,4,5,6]
</code></pre>
<br>
<h4>Construct (:)</h4>
<pre><code class="haskell">
Prelude> 0 : [1,2,3]
[0,1,2,3]
</code></pre>
<aside class="notes">The Construct (:) function is more effecient than the Concatenation function due to the implementation. If you're constructing a huge list, use the construct (:) function.</aside>
</section>
<section>
<h3>Almost <i>Everything</i> is a function</h3>
<pre><code class="haskell">
Prelude> let l1 = [1,2,3]
Prelude> let l2 = [4,5,6]
Prelude> (++) l1 l2
[1,2,3,4,5,6]
</code></pre>
<pre><code class="haskell">
Prelude> (:) 0 [1,2,3]
[0,1,2,3]
</code></pre>
<div class="fragment roll-in">
<p>In fact, a list is formally defined like this</p>
<pre><code class="haskell">
Prelude> [1,2,3] == (:) 1 ((:) 2 ((:) 3 []))
True
</code></pre>
<small>Gross! We'll put that away. I'm so sorry.</small>
</div>
<aside class="notes">Try it out. Pull up GHCi and break apart that last example.</aside>
</section>
<section>
<h3>String</h3>
<p>A String is just a list of characters.</p>
<pre><code class="haskell">
Prelude> "wahoo" == ['w', 'a', 'h', 'o', 'o']
True
</code></pre>
<br>
<p>So (++) and (:) work on strings too.</p>
<aside class="notes">A String is surrounded by double-quotation marks, and a Char is surrounded by single-quotation marks.</aside>
</section>
<section data-state="soothe">
<h3>True or False?</h3>
<pre class="fragment roll-in"><code contenteditable class="haskell">
Prelude> "" == []
</code></pre>
<pre class="fragment roll-in"><code contenteditable class="haskell">
True
</code></pre>
<pre class="fragment roll-in"><code contenteditable class="haskell">
Prelude> 'a':"bc" == ['a', 'b', 'c']
</code></pre>
<pre class="fragment roll-in"><code contenteditable class="haskell">
True
</code></pre>
<pre class="fragment roll-in"><code contenteditable class="haskell">
Prelude> 6:"789" == "6789"
</code></pre>
<pre class="fragment roll-in"><code contenteditable class="haskell">
ERROR!
</code></pre>
<aside class="notes">"Hi" is just syntactic sugar for ['H','i'], which is just syntactic sugar for (:) 'H' ((:) 'i' []). Haskell is pretty sweet.</aside>
</section>
<section>
<h3>Useful List Functions</h3>
<ul>
<li><p><code>head</code> returns the first element of a list</p></li>
<li><p><code>tail</code> returns the remaining elements of a list</p></li>
<li><p><code>last</code> returns the last element of a list</p></li>
<li><code>init</code> returns everything but the last element</li>
<center>
<img src="L02_files/listmonster.png">
<small><a target="_blank" href="http://learnyouahaskell.com/starting-out#an-intro-to-lists">image © Miran Lipovača: <i>Learn You a Haskell</i></a></small>
</center>
</ul>
<aside class="notes">Bring up GHCi and try it yourself! See what happens when you apply the `head` function to a String.</aside>
</section>
<section data-state="soothe">
<h3>Reverse a List</h3>
<p>Create a file <code>myfunction.hs</code></p>
<p>and write a function to reverse a list</p>
<pre id="a"><code contenteditable class="haskell">
rev [] = []
</code></pre>
<pre id="b" style="display:none;"><code contenteditable class="haskell">
rev [] = []
rev xs = last xs : rev (init xs)
</code></pre>
<pre id="c" style="display:none;"><code contenteditable class="haskell">
rev [] = []
rev xs = rev (tail xs) ++ [head xs]
</code></pre>
<pre id="d" style="display:none;"><code contenteditable class="haskell">
rev [] = []
rev (x:xs) = reverse xs ++ [x]
</code></pre>
<span style="font-size:12px;">
<a onclick="toggleElements('b', 'a');toggleElements('c', 'a');toggleElements('d', 'a');">(clear answer)</a>
<a onclick="toggleElements('a', 'b');toggleElements('c', 'b');toggleElements('d', 'b');">(show answer 1)</a>
<a onclick="toggleElements('a', 'c');toggleElements('b', 'c');toggleElements('d', 'c');">(show answer 2)</a>
<a onclick="toggleElements('a', 'd');toggleElements('b', 'd');toggleElements('c', 'd');">(show answer 3)</a>
</span>
<div class="fragment roll-in">
<p>run <code>ghci</code> and load the file</p>
<pre><code class="haskell">
Prelude> :load myfunction.hs
[1 of 1] Compiling Main ( myfunction.hs, interpreted )
Ok, modules loaded: Main.
*Main> rev "hello"
"olleh"
</code></pre>
</div>
<aside class="notes">This is your first time writing code outside of GHCi. Exciting! Open up notepad, emacs, vim, or any text-editor, and start writing code. Remember where you saved your file. In GHCi, make sure you're in the same directory. If you saved your file in C:\myfunction.hs, type `:cd C:\` in GHCi.</aside>
</section>
<section>
<h2><a target="_blank" href="http://www.haskell.org/hoogle/">Hoogle</a></h2><h3>the Google for Haskell</h3>
<p>The Prelude package comes with many functions.</p>
<p>Reuse, reuse, reuse!</p>
<p><a target="_blank" href="http://www.haskell.org/onlinereport/standard-prelude.html"><code>reverse</code> is already implemented for you!</a></p>
<br>
<p class="fragment roll-in"><small>GG, Haskell</small></p>
<aside class="notes">I hope you're beginning to see why Haskell is called a functional language.</aside>
</section>
<section>
<h2>List Comprehension</h2>
<p><small>Let S be a <b>set</b>, and p(x) be a <b>predicate</b>.</small></p>
<p><small>Mathematically, we can write the following statement.</small></p>
<p><code>∀x∈S, p(x)</code></p>
<br>
<p><small>Haskell allows a similar notation</small><p>
<p><code>[ x | x ← S, p x ]</code></p>
<br>
<div class="fragment roll-in">
<p><small>← is pronounced <b>"drawn from"</b></small></p>
<p><small><code>p x</code> is called a <b>guard</b></small></p>
</div>
<aside class="notes">Think of this as a for-loop.</aside>
</section>
<section data-state="soothe">
<h3>What's the output?</h3>
<pre id="e"><code class="haskell">
Prelude> [ x+5 | x <- [1,2,3] ]
</code></pre>
<pre id="f" style="display:none;"><code class="haskell">
Prelude> [ x+5 | x <- [1,2,3] ]
[6,7,8]
</code></pre>
<span style="font-size:12px;">
<a onclick="toggleElements('f', 'e');">(clear answer)</a>
<a onclick="toggleElements('e', 'f');">(show answer)</a>
</span>
<div class="fragment roll-in">
<pre id="g"><code class="haskell">
Prelude> [ x | x <- [2..10], 10 `mod` x == 0]
</code></pre>
<pre id="h" style="display:none;"><code class="haskell">
Prelude> [ x | x <- [2..10], 10 `mod` x == 0]
[2,5,10]
</code></pre>
<span style="font-size:12px;">
<a onclick="toggleElements('h', 'g');">(clear answer)</a>
<a onclick="toggleElements('g', 'h');">(show answer)</a>
</span>
</div>
<div class="fragment roll-in">
<pre id="i"><code class="haskell">
Prelude> [ team ++ " " ++ player |
team <- ["red", "blue"],
player <- ["soldier", "pyro", "scout"] ]
</code></pre>
<pre id="j" style="display:none;"><code class="haskell">
Prelude> [ team ++ " " ++ player |
team <- ["red", "blue"],
player <- ["soldier", "pyro", "scout"] ]
["red soldier","red pyro","red scout","blue soldier","blue pyro","blue scout"]
</code></pre>
<span style="font-size:12px;">
<a onclick="toggleElements('j', 'i');">(clear answer)</a>
<a onclick="toggleElements('i', 'j');">(show answer)</a>
</span>
</div>
<aside class="notes">List comprehensions are an easy way to construct lists</aside>
</section>
<section>
<h2>Tuples</h2>
<p>A tuple is a binding of data types</p>
<p>It's comma-separated, just like lists</p>
<p>A tuple is surrounded by ( ... )</p>
<br>
<ul>
<li><code class="haskell">(1, 2)</code></li>
<li><code class="haskell">("lol", "wut")</code></li>
<li><code class="haskell">(40, "forty")</code></li>
<li><code class="haskell">("Coins", [1, 5, 10, 25])</code></li>
</ul>
<aside class="notes">Tuples can contain different data types. This is useful for mapping one data to another. Note, a Tuple doesn't need to be just 2 elements.</aside>
</section>
<section>
<h3>Accessing Tuple Elements</h3>
<ul>
<li>
<p><code class="haskell">fst</code> retrieves the first element</p>
<pre><code class="haskell">
Prelude> fst (1,2)
1
</code></pre>
</li>
<li>
<p><code class="haskell">snd</code> retrieves the second element</p>
<pre><code class="haskell">
Prelude> snd (1,2)
2
</code></pre>
</li>
</ul>
<aside class="notes">fst and snd only work with types with just two elements</aside>
</section>
<!-- [f arg | f<-fs, arg<-args] -->
<section>
<h3>Tuple Examples</h3>
<pre><code class="haskell">
Prelude> [ (n, even n) | n <- [1..4] ]
[(1,False),(2,True),(3,False),(4,True)]
</code></pre>
<br>
<pre><code class="haskell">
Prelude> [(a,b,c) | c<-[1..10], b<-[1..c], a<-[1..b]
, a^2 + b^2 == c^2]
[(3,4,5),(6,8,10)]
</code></pre>
<aside class="notes">We can use tuples with list comprehension to create some interesting things.</aside>
</section>
<section>
<h2>Editors</h2>
<p>Grab <a target="_blank" href="http://www.haskell.org/haskellwiki/Editors">one of these editors</a>, and start hacking!</p>
<aside class="notes">You're almost ready!</aside>
</section>
<section>
<h1>Homework</h1>
<h2>Think Differently</h2>
<ol>
<li>
<p>Write 2 different implementations of <a href="http://en.wikipedia.org/wiki/Factorial">factorial</a></p>
<pre><code class="haskell">
facA n = ...
facB n = ...
</code></pre>
</li>
<li>
<p>Save the file as <code class="haskell">fac.hs</code> and run it using GHCi</p>
</li>
<li><p><a href="https://docs.google.com/spreadsheet/viewform?formkey=dHpELS1oeFNvc0VxUS1xbkxIQVlYOWc6MQ">Fill out this week's form</a></p></li>
</ol>
<aside class="notes">Don't forget to fill out the form!</aside>
</section>
</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>