-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlec10.html
More file actions
477 lines (399 loc) · 15.4 KB
/
lec10.html
File metadata and controls
477 lines (399 loc) · 15.4 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Haskell - Lecture 10</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 10</p>
<p>
<br>
</p>
<p>Haskell Development Methods</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>
<section>
<h2>Good Coding Style</h2>
<ul>
<li>Follow the <a href="http://www.emacswiki.org/emacs/EightyColumnRule" target="_blank">80 column rule</a></li>
<br>
<li class="fragment roll-in">
<p>Avoid <a href="http://xkcd.com/297/" target="_blank">too many parentheses</a></p>
<ul>
<li><a href="http://shuklan.com/haskell/lec06.html#/0/13" target="_blank">Use (<code>$</code>)</a></li>
<li><a href="http://shuklan.com/haskell/lec08.html#/0/17" target="_blank">Use (<code>.</code>)</a></li>
</ul>
</li>
<br>
<li class="fragment roll-in">Use <code>{- -}</code> for large multi-lined comments</li>
<br>
<li class="fragment roll-in">Use <a href="http://learnyouahaskell.com/making-our-own-types-and-typeclasses" target="_blank">Type Synonyms</a> to make code readable</li>
</ul>
<aside class="notes">Since there are often multiple ways to design an implementation in Haskell, focus on simplicity and readability.</aside>
</section>
<section>
<h2>Point-Free Notation</h2>
<p>Some functions can be written without variables</p>
<table style="width:100%;">
<tr>
<td style="width:50%;">
<pre><code>
add a b = a + b
</code></pre>
</td>
<td style="width:50%;">
<pre><code>
add = (+)
</code></pre>
</td>
</tr>
<tr><td> </td><td></td></tr>
<tr>
<td>
<pre><code>
f s = length (head (words s))
</code></pre>
</td>
<td>
<pre><code>
f = length.head.words
</code></pre>
</td>
</tr>
</table>
<br>
<small>Compose functions (high level) rather than shuffle data (low level) <a href="http://www.haskell.org/haskellwiki/Pointfree" target="_blank">*</a></small>
<aside class="notes">Point-free is also called Point-less, but it is certainly not pointless. This notation emphasizes 'the what' rather than 'the how'.</aside>
</section>
<section data-state="soothe">
<h3>Point-Free is <a href="http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf" target="_blank">Exciting!</a></h3>
<p>Convert this piece of code to point-free notation</p>
<pre><code>
f x = 5 + 8/x
</code></pre>
<small>
<pre class="fragment roll-in"><code>
f x = (+) 5 ( (/) 8 x)
</code></pre>
<pre class="fragment roll-in"><code>
f x = ((+) 5) ( ((/) 8) x)
</code></pre>
<pre class="fragment roll-in"><code>
f x = ((+) 5) . ( ((/) 8) ) x
</code></pre>
<pre class="fragment roll-in"><code>
f = ((+) 5) . ( (/) 8 )
</code></pre>
<pre class="fragment roll-in"><code>
f = (5+) . (8/)
</code></pre>
</small>
<p><small><a href="http://stackoverflow.com/questions/8465575/simple-haskell-functions-in-point-free-style">(solution by Prateek)</a></small></p>
<aside class="notes">As you can see, point-free changes the way your think. The implementation of the function is now just a composition of other functions.</aside>
</section>
<section>
<h2>Haskell File Structure</h2>
<ul>
<li>
<p>/src</p>
<small>Folder for source-code</small>
</li>
<li>
<p>Setup.hs</p>
<small>Haskell build script</small>
</li>
<li>
<p>app.cabal</p>
<small>Cabal build script</small>
</li>
<li>
<p>README</p>
<small>Informative text file</small>
</li>
<li>
<p>LICENCE</p>
<small>Software licence statement</small>
</li>
</ul>
<aside class="notes">These are just convention.</aside>
</section>
<section>
<h3>Make a folder called <code>src</code></h3>
<p>Create a <code>Main.hs</code> file in /src</p>
<pre><code>
-- Main.hs
module Main where
main = do
putStrLn "What's your name?"
name <- getLine
putStrLn ("Hi " ++ name)
</code></pre>
<aside class="notes">Although we could simply run this code through ghc, running more complicated code should be done another way (demonstrated in the following slides).</aside>
</section>
<section>
<h2>Set Up the Rest</h2>
<p>Return to the top directory, and run <code>cabal init</code>.</p>
<p>It helps you fill out the following information:</p>
<ul><small>
<li>Package name</li>
<li><a href="http://www.haskell.org/haskellwiki/Package_versioning_policy">Package version</a></li>
<li>License</li>
<li>Author name</li>
<li>Maintainer email</li>
<li>Project homepage URL</li>
<li>Project synopsis</li>
<li>Build type</li>
</small>
</ul>
<br>
<br>
<p><a href="https://github.com/CGenie/haskell-snake/blob/master/snake.cabal" target="_blank">Here's an example of a good cabal file</a></p>
<aside class="notes">cabal is the main tool we will use. `cabal` comes with the Haskell-platform. If you're on a Debian based machine, just do 'sudo apt-get install cabal-install'.</aside>
</section>
<section>
<h3>Almost done</h3>
<p>Your file structure should now look like:</p>
<ul>
<li>/src</li>
<li>Setup.hs</li>
<li><i>projectName</i>.cabal</li>
<li>LICENCE</li>
</ul>
<br>
<br>
<p>Edit the last part of <i>projectName</i>.cabal:</p>
<pre><code>
executable MyProj
main-is: Main.hs
-- other-modules:
build-depends: base ==4.6.*
hs-source-dirs: src
</code></pre>
<aside class="notes">You'll need to define the Main.hs file containing the main function.</aside>
</section>
<section>
<h3>That's it!</h3>
<p>Run <code>cabal install</code> at the project directory</p>
<pre><code class="cpp">
$ cabal install
Resolving dependencies...
Configuring MyProj-0.1.0.0...
Building MyProj-0.1.0.0...
Preprocessing executable 'MyProj' for MyProj-0.1.0.0...
[1 of 1] Compiling Main
Linking dist/build/MyProj/MyProj ...
Installing executable(s) in /home/binroot/.cabal/bin
Installed MyProj-0.1.0.0
</code></pre>
<small>Your executable is ready in <code>~/.cabal/bin</code></small>
<aside class="notes">We now have an executable file ready!</aside>
</section>
<section>
<h2><a href="http://www.haskell.org/haddock/doc/html/markup.html#id564988" target="_blank">Documentation</a></h2>
<p>Open up <code>Main.hs</code> and add some documentation</p>
<pre><code>
module Main where
-- |This is the main function
main :: IO()
main = do
putStrLn "What's your name?"
name <- getLine
putStrLn ("Hi " ++ name)
</code></pre>
<p><code>-- |</code> is the syntax for documenting function type signature, type declaration, or class declaration</p>
<aside class="notes">Sometimes the code speaks for itself, otherwise you should document the code.</aside>
</section>
<section>
<h2>Run Haddock</h2>
<p>Haddock is a documentation engine.</p>
<p>Run <code>cabal haddock --executables</code></p>
<br>
<p>Your documentation will be ready in dist/doc/html/MyProj/MyProj/<b>index.html</b></p>
<br>
<p class="fragment roll-in">Congratulations! Soon your documentation will look like <a href="http://hackage.haskell.org/package/hgeometry" target="_blank">this</a></p>
<aside class="notes">It's similar to javadoc.</aside>
</section>
<section data-state="soothe">
<h2>Try it yourself</h2>
<p>Rewrite the <a href="http://shuklan.com/haskell/lec07.html#/0/2" target="_blank">password-strength checker</a> using the proper code structure</p>
<br>
<p>Make sure you have the following:</p>
<small>
<ul>
<li>
<p>/src/Main.hs</p>
</li>
<li>
<p>Setup.hs</p>
</li>
<li>
<p>app.cabal</p>
</li>
<li>
<p>README</p>
</li>
<li>
<p>LICENCE</p>
</li>
</ul>
</small>
<br>
<br>
<p>Don't forget to <a href="http://www.haskell.org/haddock/doc/html/markup.html#id564988" target="_blank">document</a> your code</p>
<aside class="notes">Don't feel overwhelmed by the amount of files in your folder. The only files that really matter for you are in the /src folder.</aside>
</section>
<section>
<h1><a href="https://github.com/BinRoot/PasswordStrength" target="_blank">Solution</a></h1>
<aside class="notes">Here's an example.</aside>
</section>
<section>
<h3>Learn from Others</h3>
<p>You now have the skills to soak in other people's Haskell code</p>
<br>
<p>Check out this simple <a href="https://github.com/CGenie/haskell-snake" target="_blank">Snake game</a></p>
<small>Download it. Run <code>cabal install</code> and play around with the code.</small>
<aside class="notes">Notice how you can understand most of the source code. If fact you can strip out functions from the source code and use them in your own projects. Haskell's type system is your best friend.</aside>
</section>
<section>
<h2>Use Version Control!</h2>
<h3><a href="http://try.github.com">Git</a> is Awesome. Use <a href="http://git-scm.com/book">Git</a>.</h3>
<br>
<p>If you're an uber-haxx0r, use <a href="http://darcs.net/">Darcs</a></p>
<small>(It's written in Haskell)</small>
<br>
<br>
<p class="fragment roll-in">Regardless, start using version control. Become a developer, not just a programmer.</p>
<aside class="notes">Version controlling is not just useful for yourself to keep track of changes, but also irreplaceable for working with a team of programmers.</aside>
</section>
<section>
<h1>Homework</h1>
<h2>Learn from Others</h2>
<ol>
<li>
<a href="https://docs.google.com/forms/d/1TbQaw-2r0Ow6wtn8_L8Mey4mcmWm0qO3Ylw1C_zQ2gA/viewform" target="_blank">Fill out this week's form!</a>
</li>
<li>
<p>Find an interesting piece of Haskell code online and run it.</p>
<p>Hint: <a target="_blank" href="https://github.com/languages/Haskell">Browse GitHub</a></p>
</li>
</ol>
</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>