-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlec08.html
647 lines (533 loc) · 19.1 KB
/
lec08.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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Haskell - Lecture 8</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 8</p>
<p>
<br>
</p>
<p>Input/Output</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('intersectA').style.display = 'none';
document.getElementById('intersectB').style.display = 'none';
}
function showOne(intersect) {
clearAll();
document.getElementById(intersect).style.display = 'block';
}
</script>
<section>
<h3>Review of Homework 7</h3>
<small><a href="lec07.html#/0/20" target="_blank">Find the intersect of two lines using an existing package</a></small>
<table style="width:100%">
<tr>
<td><center><a onclick="showOne('intersectA');">A</a></center></td>
<td><center><a onclick="showOne('intersectB');">B</a></center></td>
</tr>
</table>
<pre id='intersectA'><code class='haskell'>
-- Designed by Anish S. Tondwalkar
import Graphics.Gloss.Data.Point
import Graphics.Gloss.Geometry.Line
-- A line is specified by two points (a point is a pair of Floats)
data Line = L Point Point
-- takes two lines instead of two points
intersectLines (L a b) (L c d) = intersectLineLine a b c d
</code></pre>
<pre id='intersectB' style="display:none;"><code class='haskell'>
-- Designed by Jonathan E Hills
import Roots
import Data.Complex
-- Find the intersection of the lines
-- y = m1 * x + b1 and y = m2 * x + b2 by getting the roots
-- of the difference of the polynomials
-- b1 + m1 * x and b2 + m2 * x
-- findIntersection (m1, b1) (m2, b2) gives (x, y)
findIntersection :: (Double, Double) -> (Double, Double) -> (Double, Double)
findIntersection (m1, b1) (m2, b2)
| m1 == m2 = error ""Lines are parallel!""
| otherwise = (x, m1 * x + b1) where
x = Data.Complex.realPart (r !! 0) where
r = roots 0.00001 10000 [(b1 - b2) :+ 0, (m1 - m2) :+ 0]
</code></pre>
</section>
<section>
<h3>Pure</h3>
<p>Remember, Haskell is pure.</p>
<br>
<ul>
<li>Functions can't have side-effects</li>
<li>Functions take in inputs and compute outputs</li>
<li>Nothing else happens in-between <p>(no modification of global variables)</p></li>
</ul>
<br>
<br>
<small class="fragment roll-in">However, input/output is not at all pure. There is a side effect! By displaying output pixels on your monitor, you are changing the global state of your computer.</small>
<aside class="notes">It would be difficult to make anything useful without side-effects.</aside>
</section>
<section>
<h2>Output</h2>
<p>Let's print out some output!</p>
<p>Create a file <code class="haskell">Main.hs</code> and write the following code</p>
<pre><code class="haskell">
module Main where
main = do
putStrLn "Hello world!"
putStrLn "main is a function"
putStrLn "of type IO ()"
</code></pre>
<br>
<p>Run the code</p>
<small>
<table border="1">
<tr>
<td width="33%" class="fragment roll-in">
<p>Use GHCi</p>
<pre><code class="haskell">
$ ghci
Prelude> :load Main.hs
*Main> main
</code></pre>
</td>
<td width="33%" class="fragment roll-in">
<p>Or just use <code class="haskell">runhaskell</code></p>
<pre><code class="haskell">
$ runhaskell Main.hs
</code></pre>
</td>
<td width="33%" class="fragment roll-in">
<p>Or compile the code!</p>
<pre><code class="haskell">
$ ghc --make Main.hs
$ ./Main
</code></pre>
</td>
</tr>
</table>
</small>
<aside class="notes">Notice the two new ways of running Haskell code. We've been using GHCi the whole time, but this is how you compile it.</aside>
</section>
<section>
<h3><code class="haskell">main</code></h3>
<p><code class="haskell">main</code> is the "entry point of a Haskell program" <a target="_blank" href="http://www.haskell.org/tutorial/io.html">*</a></p>
<p>It must be an IO type.</p>
<pre><code class="haskell">
$ ghci Main.hs
*Main> :t main
main :: IO ()
</code></pre>
<p>It's like the <code class="haskell">main</code> from Java or C++. When you compile Haskell code, the <code class="haskell">main</code> function runs.</p>
<aside class="notes">main has the type IO () Monad.</aside>
</section>
<section>
<h3><code class="haskell">do</code></h3>
<p>glues together multiple IO actions</p>
<p><code class="haskell">do</code> is a keyword in Haskell, like <code class="haskell">let</code> or <code class="haskell">where</code>.</p>
<pre><code class="haskell">
main = do
putStrLn "this line is the first IO action"
putStrLn "putStrLn has type String -> IO ()"
putStrLn "we are glueing together multiple IO ()"
</code></pre>
<aside class="notes">`putStrLn` produce IO Monads, and we use `do` to glue them together.</aside>
</section>
<section>
<h2>Input</h2>
<p>Input is just as straight forward.</p>
<p>The <code class="haskell">getLine</code> function binds input to a variable.</p>
<pre><code class="haskell">
module Main where
main = do
putStrLn "Why did the banker leave his job?"
answer <- getLine
putStrLn (if answer == "he lost interest"
then "Correct!"
else "Wrong!")
</code></pre>
<aside class="notes">putStrLn takes in a String as input, so our if-then-else function appropriately returns a String.</aside>
</section>
<section>
<h3><code class="haskell"><-</code></h3>
<p>We have seen the <code class="haskell"><-</code> symbol before</p>
<pre><code class="haskell">
[ x | x<-[1..10], even x ]
</code></pre>
<p>The symbol is pronounced "drawn from"</p>
<br>
<pre><code class="haskell">
main = do
input <- getLine
putStrLn ("you wrote: " ++ input)
</code></pre>
<p>"input is drawn from getLine"</p>
<aside class="notes">Really get to know this piece of code. Open up your text editor and start writing it. Play around with it.</aside>
</section>
<section>
<h2>IO</h2>
<p>If you see IO, think side effects.</p>
<p><code class="haskell">putStrLn</code> displays something to your monitor. That's a side effect!</p>
<br>
<p>putStrLn gives back nothing, ()</p>
<pre><code class="haskell">
Prelude> :t putStrLn
putStrLn :: String -> IO ()
</code></pre>
<br>
<p>getLine gives back a String to use</p>
<pre><code class="haskell">
Prelude> :t getLine
getLine :: IO String
</code></pre>
<aside class="notes">The IO Monad "gives back" a result to use, bundled in a monad.</aside>
</section>
<section>
<h3>Pure</h3>
<p class="fragment roll-in">Side-effects are isolated into I/O actions.</p>
<p class="fragment roll-in">Pure code is separated from impure operations.</p>
<p class="fragment roll-in">I/O actions only exist within other I/O actions.</p>
<div class="fragment roll-in">
<br>
<h3>Brilliant!</h3>
<img src="L08_files/brilliant.gif">
</div>
<aside class="notes">We're dealing with a dangerous creature, side-effects. Haskell is still pure, even after introducing input and output. Thank the dozens of researchers around the world for that.</aside>
</section>
<section>
<h3><code class="haskell">return</code></h3>
<p><code class="haskell">return</code> is a function that "makes an I/O action out of a pure value" <a target="_blank" href="http://learnyouahaskell.com/input-and-output">*</a></p>
<pre><code class="haskell">
Prelude> :t return
return :: Monad m => a -> m a
</code></pre>
<br>
<p><code class="haskell">return "hello" :: IO String</code></p>
<br>
<p>It's pretty much the opposite of the <code class="haskell"><-</code> syntax.</p>
<pre><code class="haskell">
main = do
input <- return "hello"
putStrLn input
</code></pre>
<p><small><code class="haskell">return</code> packs up a value into an IO box. <code class="haskell"><-</code> extracts the value out of an IO box.</small></p>
<aside class="notes">The return function is nothing like the one you've used in Java or C++.</aside>
</section>
<section>
<h3>Using <code class="haskell">return</code></h3>
<p>In this program, we exit when the input is "y"</p>
<pre><code class="haskell">
module Main where
main = do
putStrLn "quit the program? y/n"
ans <- getLine
if ans /= "y" then do
putStrLn "not quitting"
main
else return ()
</code></pre>
<p><b>return ()</b> has the type <b>IO ()</b></p>
<p>We create an IO that doesn't do anything so that the program could exit.</p>
<aside class="notes">By necessity, we need an empty IO monad to finish the main function.</aside>
</section>
<section>
<h3><code class="haskell">when</code></h3>
<p>The <b>when</b> function from Control.Monad module looks nicer</p>
<br>
<p><code class="haskell">when :: Monad m => Bool -> m () -> m ()</code></p>
<br>
<table width="100%">
<tr>
<td><center>Old code</center></td>
<td><center>New code</center></td>
</tr>
<tr>
<td>
<pre><code class="haskell">
module Main where
main = do
putStrLn "quit? y/n"
ans <- getLine
if ans /= "y" then do
putStrLn "not quitting"
main
else return ()
</code></pre>
</td>
<td>
<pre><code class="haskell">
module Main where
import Control.Monad
main = do
putStrLn "quit? y/n"
ans <- getLine
when (ans /= "y") $ do
putStrLn "not quitting"
main
</code></pre>
</td>
</tr>
</table>
<aside class="notes">Haskell code is surprisingly readable.</aside>
</section>
<section>
<h3><code class="haskell">sequence</code></h3>
<p><b>sequence</b> evaluates each IO action from a list of actions and returns a list of IO outputs</p>
<br>
<p><code class="haskell">sequence :: Monad m => [m a] -> m [a]</code></p>
<br>
<pre><code class="haskell">
Prelude> sequence [getLine, getLine]
hello
world
["hello","world"]
</code></pre>
<pre><code class="haskell">
Prelude> sequence (map print [1,2])
1
2
[(),()]
</code></pre>
<p><small><b>print</b> is equivalent to <b>putStrLn.show</b></small></p>
<aside class="notes">You can easily mix pure and impure concepts in Haskell.</aside>
</section>
<section>
<h3><code class="haskell">mapM</code></h3>
<p><b>mapM</b> takes care of the sequencing stuff</p>
<pre><code class="haskell">
Prelude> mapM print [1,2,3]
1
2
3
[(),(),()]
</code></pre>
<div class="fragment roll-in">
<br>
<p>and <b>mapM_</b> is the same but doesn't output anything afterwards</p>
<pre><code class="haskell">
Prelude> mapM_ print [1,2,3]
1
2
3
</code></pre>
</div>
<aside class="notes">There are a lot of useful functions!</aside>
</section>
<section>
<h3><code class="haskell">interact</code></h3>
<p><b>interact</b> makes I/O really easy.</p>
<p>Just call <code class="haskell">interact</code> on a <i>String -> String</i> function and you're done!</p>
<br>
<small>This code counts the number of characters per line</small>
<pre><code class="haskell">
module Main where
main = interact countChars
countChars :: String -> String
countChars str =
let allLines = lines str
lengths = map (show.length) allLines
in unlines lengths
</code></pre>
<aside class="notes">Notice how I separate the impure stuff from the pure haskell code. The main function handling the ugly IO, and the countChars function is pure.</aside>
</section>
<section>
<h2>File Input</h2>
<small>Let's count the number of lines in a file,</small>
<span style="font-size:20px;">
<a onclick="toggleElements('b', 'a');">(<b>verbose</b>)</a>
<a onclick="toggleElements('a', 'b');">(<b>elegant</b>)</a>
</span>
<div id="a">
<small>It's best to keep the pure code separate from the ugly IO stuff</small>
<pre><code class="haskell">
module Main where
import System.IO
main = do
theInput <- readFile "input.txt"
putStrLn (countLines theInput)
countLines :: String -> String
countLines str = (show (length (lines str)))
</code></pre>
</div>
<div id="b" style="display:none;">
<small>One line. Checkmate.</small>
<pre><code class="haskell">
module Main where
import System.IO
main = readFile "input.txt" >>= print.length.lines
</code></pre>
<p><img src="L08_files/checkmate.gif"></p>
</div>
<aside class="notes">The code is beautiful.</aside>
</section>
<section>
<h2>File Output</h2>
<p>Writing to file is done with <b>writeFile</b></p>
<pre><code class="haskell">
module Main where
import System.IO
main = do
putStrLn "writing to file..."
writeFile "output.txt" ['A'..'Z']
</code></pre>
<small><b>writeFile</b> will overwrite the file. Use <b>appendFile</b> if you'd like to append instead</small>
<aside class="notes">The code is very "English-like"</aside>
</section>
<section>
<h1>Homework</h1>
<h2>Wrestling with Haskell</h2>
<ol>
<li><a href="https://docs.google.com/forms/d/1ImwxVJh4riFDb6wxdVTkGxLa5Cpha7wnHE0TBTrp8sI/viewform" target="_blank">Fill out this form!</a></li>
<li>Research 5 facts about Monads</li>
<li>
<p>Write an IO program that reverses text</p>
<small>given a file containing some text, create an output file with everything in reverse</small>
<br>
<br>
<small>
<table>
<tr>
<td>input.txt</td>
<td>output.txt</td>
</tr>
<tr>
<td>
<pre><code class="no-highlight">
hello world
</code></pre>
</td>
<td>
<pre><code class="no-highlight">
dlrow olleh
</code></pre>
</td>
</tr>
</table>
</small>
</li>
</ol>
<aside class="notes">This is actually much easier than it appears. A good amount of the code is in the slides.</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>