-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfp101.html
1642 lines (1390 loc) · 44.1 KB
/
fp101.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Functional Programming 101</title>
<meta name="author" content="(Nathan Dotz)"/>
<style type="text/css">
.underline { text-decoration: underline; }
</style>
<link rel="stylesheet" href="file:///Users/sleepynate/Dropbox%20(Personal)/org-mode/reveal.js-3.3.0/css/reveal.css"/>
<link rel="stylesheet" href="file:///Users/sleepynate/Dropbox%20(Personal)/org-mode/reveal.js-3.3.0/css/theme/moon.css" id="theme"/>
<link rel="stylesheet" href="file:///Users/sleepynate/Dropbox%20(Personal)/org-mode/reveal.js-3.3.0/lib/css/zenburn.css"/>
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'file:///Users/sleepynate/Dropbox%20(Personal)/org-mode/reveal.js-3.3.0/css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="sec-title-slide"><h1 class="title">Functional Programming 101</h1><h2 class="author">Nathan Dotz</h2><h2 class="date">Codemash 2017</h2>
</section>
<section>
<section id="slide-org2f9e751">
<h2 id="org2f9e751">Overview</h2>
<aside class="notes">
<ul>
<li>Our objective for this workshop is going to be to learn the
concepts behind functional programming</li>
<li>To do this, we're going to review 2 seminal functional programming
languages, ML and Racket</li>
<li>Along the way, I will guide you through several exercises that
we'll do as a group so you don't have to worry about setting up
archaic programming environments on your machine</li>
<li>Once we've reviewed ML and Racket, we'll spend the remainder of
the session presenting practice problems and giving you time to
work on them in the language you normally work in, focusing on how
to create functional-styled solutions.</li>
</ul>
</aside>
<ul>
<li>Learn Functional Programming from scratch</li>
<li>Static & strongly typed functional language (ML)</li>
<li>Weak & dynamically typed functional language (Racket)</li>
<li>Work on using concepts we learned in your language</li>
</ul>
</section>
</section>
<section>
<section id="slide-orgb5ef6b3">
<h2 id="orgb5ef6b3">Do your best to</h2>
<aside class="notes">
<ul>
<li>You might be asking yourself why we're using two languages that
you're not likely to ever use again once you leave here.</li>
<li>That's actually the whole point! Try to not think about the
languages that you are familiar with and instead think about these
languages on their own, which is the best way to let new ideas
sink in</li>
<li>We'll worry about how to apply the methodologies we have learned
from ML and Racket when we get to the exercises section.</li>
</ul>
</aside>
<ul>
<li>Ignore the FP concepts you've tried to learn already</li>
<li>Not compare to your normal language</li>
<li>Treat these as completely new concepts</li>
</ul>
</section>
</section>
<section>
<section id="slide-org2acdeb9">
<h2 id="org2acdeb9">What are we learning today?</h2>
<aside class="notes">
<p>
Before we get too involved, let's get to know each other a little,
which will also help me to know what topics to concentrate on more.
</p>
<p>
First, we'll have everyone put their hands up, and I'm going to
recite a list of successively fancier programming terms and
concepts, and as I list them off, if you understand that concept,
leave your hand up in the air.
</p>
<p>
If you don't understand a concept or term, or maybe you're familiar
but not quite comfortable with it, take your hand down and if you're
next to someone who has their hand up, introduce yourselves.
</p>
<p>
You're all going to be talking to each other and me during exercises
anyways so just get used to helping each other a little bit.
</p>
<p>
OK, hands up.
</p>
<ul>
<li>variable</li>
<li>function</li>
<li>object</li>
<li>class</li>
<li>constant</li>
<li>compiler</li>
<li>interpreter</li>
<li>REPL</li>
<li>recursion</li>
<li>lambdas/anonymous functions</li>
<li>higher-order functions</li>
<li>tail-call recursion optimization</li>
<li>lexical scoping</li>
<li>referential transparency</li>
<li>functor</li>
<li>applicative functor</li>
<li>monad</li>
<li>Kleisli triple</li>
<li>zygohistomorphic prepromorphisms</li>
</ul>
</aside>
</section>
</section>
<section>
<section id="slide-org90b20af">
<h2 id="org90b20af">Standard ML</h2>
<div class="outline-text-2" id="text-org90b20af">
</div></section>
<section id="slide-orgcc9337b">
<h3 id="orgcc9337b">ML</h3>
<aside class="notes">
<ul>
<li>ML was thought up by Robin Milner in 1973</li>
<li>ML was not intended for implementation</li>
<li>Rather, ML was supposed to be a "Meta Language" to write proofs in</li>
<li>Showing up on the scene about 15 years after LISP, ML is often
referred to as "LISP with types", although as we'll see, ML can
also be quite different than LISP</li>
</ul>
</aside>
<ul>
<li>Robin Milner, 1973 University of Edinburgh</li>
<li>Language spec, not implementation</li>
<li>"LISP with types"</li>
</ul>
</section>
<section id="slide-org7a88265">
<h3 id="org7a88265">Standard ML</h3>
<aside class="notes">
<ul>
<li>Of course, if you put something out into the world that's never
supposed to be implemented, somebody is going to implement it</li>
<li>Those somebodies were the Princeton & Bell Labs</li>
</ul>
</aside>
<ul>
<li>Proposed 1983</li>
<li>Defined 1990</li>
<li>Revised 1997</li>
<li>Implemented by Princeton & Bell Labs as SML/NJ</li>
<li>Grandparent of OCaml, Haskell, F#, Elm, Idris</li>
</ul>
</section>
<section id="slide-org5ac3b61">
<h3 id="org5ac3b61">Features of ML</h3>
<ul>
<li>Functional programming (but not pure)</li>
<li>Tail-call optimization</li>
<li>Call-by-Value</li>
<li>Eager evaluation</li>
<li>Pattern matching</li>
<li>Garbage collection</li>
<li>Static typing</li>
<li>Type inference</li>
</ul>
</section>
</section>
<section>
<section id="slide-orgde7e4e6">
<h2 id="orgde7e4e6">Racket</h2>
<div class="outline-text-2" id="text-orgde7e4e6">
</div></section>
<section id="slide-org9e9d407">
<h3 id="org9e9d407">LISP</h3>
<aside class="notes">
<ul>
<li>LISP started all the way back in 1958 with John McCarthy</li>
<li>LISP is the second oldest programming language still in
widespread use today. Fortran is older by one year.</li>
<li>Perhaps unsurprisingly LISP started not as an implementation but
was meant to be a mathematical notation for talking about
computer programs. It subsequently implemented by a fellow named
Steve Russel sometime between '60 and '62</li>
</ul>
</aside>
<ul>
<li>John McCarthy, 1958 MIT</li>
<li>2nd oldest programming language</li>
<li>Started as a mathematical notation</li>
<li>LISt Processing</li>
<li>All program code comprised of lists (s-expressions)</li>
</ul>
</section>
<section id="slide-orgcbc5e28">
<h3 id="orgcbc5e28">Scheme</h3>
<aside class="notes">
<ul>
<li>Around the time Milner was coming up with with ML, Guy Steele &
Gerald Sussman were over at MIT's AI Lab getting frustrated with
understanding concurrency, and wrote a "tiny LISP interpreter"</li>
<li>What they ended up creating was one of the two main branches of
LISP Scheme.</li>
<li>Of its 23 reserved words, 11 are just there for convenience and
are really just combinations of the other 12 that are so common
there's no reason not to put them in the language.</li>
<li>In 1995, Netscape recruited a fellow named Brendan Eich with the
intention of having him implement Scheme as a programming
environment in their browser for creating dynamic web
applications. However, they were also working with Sun
Microsystems at the time, so it was ordained that the syntax be
changed ever so slightly to resemble Java, and 10 days later,
JavaScript was born.</li>
</ul>
</aside>
<ul>
<li>Guy Steele & Gerald Sussman, 1975 MIT AI Lab</li>
<li>Small language (23 symbols, 11 are "convenience")</li>
<li>About 45 current implementations</li>
<li>Influenced Javascript, Ruby, Clojure</li>
</ul>
</section>
<section id="slide-orgc81a519">
<h3 id="orgc81a519">Features of Racket</h3>
<ul>
<li>Functional programming (also not pure)</li>
<li>Tail-call optimization</li>
<li>Green and OS threads</li>
<li>Lexical Closures</li>
<li>Continuations</li>
<li>Powerful macro system</li>
<li>Pattern matching</li>
<li>Contracts</li>
</ul>
</section>
</section>
<section>
<section id="slide-org0aa361c">
<h2 id="org0aa361c"><img src="http://i.imgur.com/mSIUhtX.jpg" alt="mSIUhtX.jpg" /></h2>
<aside class="notes">
<p>
You may be feeling like this right now with all the jargon I just
spewed at you, and that's OK.
</p>
<p>
If all of those features that we just talked about don't make sense,
that's fine. We'll be covering many of them, if not by name, as we
present these languages.
</p>
</aside>
</section>
</section>
<section>
<section id="slide-org1fb56ee">
<h2 id="org1fb56ee">What is Functional Programming?</h2>
<aside class="notes">
<p>
OK, now is your first obligatory participation opportunity.
Does anyone have a definition of functional programming?
What concepts do you associate with functional programming?
</p>
</aside>
</section>
<section id="slide-org668df44">
<h3 id="org668df44">Did you guess</h3>
<ul>
<li class="fragment roll-in">Lambdas λ</li>
<li class="fragment roll-in">Higher-order functions</li>
<li class="fragment roll-in">Functions are first-class citizens</li>
</ul>
</section>
<section id="slide-org0b87a42">
<h3 id="org0b87a42">Well sure, but what we're after is</h3>
<ul>
<li class="fragment roll-in">elimination of state</li>
<li class="fragment roll-in">elimination of effects</li>
</ul>
<p>
via
</p>
<ul class="fragment appear">
<li>higher order functions</li>
<li>referential transparency</li>
</ul>
</section>
<section id="slide-orgddff3fd">
<h3 id="orgddff3fd">In short</h3>
<ul>
<li>pass everything into scope</li>
<li>every scope returns a value</li>
<li>scope? yep, probably functions</li>
<li>don't mutate (esp. not out of scope)</li>
</ul>
</section>
</section>
<section>
<section id="slide-org60125f0">
<h2 id="org60125f0">ML</h2>
<aside class="notes">
nil
</aside>
</section>
<section id="slide-org9d8eb25">
<h3 id="org9d8eb25">Values</h3>
<aside class="notes">
<ul>
<li>"val" is our keyword for declaring values in ML</li>
<li>ML has type inference, so we don't have to specify the types of
our declarations, but if we wish to, we can append a colon and
then the type to make sure the type infer-er behaves the way we
would like.</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >(* exactly the same *)
val a = 5
val b: int = 5
a = b (* returns true - comparison, not assignment *)
val a = 6 (* completely redeclare a - don't do this *)
</code></pre>
</div>
</section>
<section id="slide-orge9ddab0">
<h3 id="orge9ddab0">Pairs</h3>
<aside class="notes">
<ul>
<li>Tuples are heterogeneous typed and fixed-length</li>
<li>Tuples and they can be nested</li>
<li>We use hash functions to access parts of tuples</li>
<li>Accessing values with hash functions is 1-based, not 0-based</li>
<li>This is also our first look at calling functions. As you can see
here, to call a function, we just put a space between a function
and its arguments, though sometimes we need parenthesis to have
the grouping work right, as they're right-associative.</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >val a: int * int = (5, 7)
val b: int * (bool * int) = (3, (true, 2))
#1 a = 5
#2 a = 7
#1 (#2 b) = true
</code></pre>
</div>
</section>
<section id="slide-org40b60a7">
<h3 id="org40b60a7">Records</h3>
<aside class="notes">
<ul>
<li>Records are another heterogeneously typed structure</li>
<li>Again, we access values through hash functions on the keys</li>
<li>Tuples are in fact just records with numeric keys</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >val x: {a:int, b:string, c:bool} = {a=1, b="2", c=false}
val y = {foo=5}
#a x + #foo y = 6
val triple: string * bool * int = {2=false, 1="a", 3=5}
</code></pre>
</div>
</section>
<section id="slide-org7402056">
<h3 id="org7402056">Lists</h3>
<aside class="notes">
<ul>
<li>Lists are homogeneously typed and variable-length</li>
<li>The null function is our empty check for lists</li>
<li>hd and tl return the first and remaining elements of lists</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >val a: 'a list = []
val b: int list = [1, 2, 3]
val c = 0 :: b (* cons operator *)
c = [0, 1, 2, 3]
null [] = true
null b = false
hd b = 1
tl b = [2, 3]
tl (tl (tl b)) = []
</code></pre>
</div>
</section>
<section id="slide-org8be9939">
<h3 id="org8be9939">Functions</h3>
<aside class="notes">
<p>
Here we see a couple functions defined.
</p>
<ul>
<li>Parameter types occur after parameters, separated by colon</li>
<li>ML is expression-based</li>
<li>Expressions are type safe</li>
<li>∴ every if has a then and else, both returning same type</li>
<li>function return type follows parameters, separated by colon</li>
<li>function parameters are tuples</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >(* int -> int *)
fun add1 (x: int) = x + 1
(* int * int -> int *)
fun pow (x: int, y: int) =
if y = 0
then 1
else x * pow(x, y - 1)
fun cube (x: int): int = pow (x, 3)
val b = (2, 3)
pow b = 8
pow b = pow(2,3)
</code></pre>
</div>
</section>
<section id="slide-org84df0f3">
<h3 id="org84df0f3">Recursion</h3>
<aside class="notes">
<ul>
<li>No "for" loops in SML</li>
<li>Recursion is standard for iteration</li>
<li>These functions are not tail-call recursive, meaning they could
blow the stack, but we'll talk more about that in a moment.</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun sum (xs: int list) =
if null xs
then 0
else hd xs + sum (tl xs)
fun concat (xs: int list, ys: int list) =
if null xs
then ys
else hd xs :: concat (tl xs, ys)
</code></pre>
</div>
</section>
<section id="slide-orgfbfa4cc">
<h3 id="orgfbfa4cc">Local bindings</h3>
<aside class="notes">
<ul>
<li>Lexical scoping gives us local bindings</li>
<li>local bindings unavailable outside function</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun local_bindings (x: int) =
let val a = if x > 0 then x else ~x (* same as: abs x *)
val b = a + 100
in
if b > 200 then b div 2 else b * b
end
fun range (x: int) =
let
fun range (y: int) =
if y = x
then x :: []
else y :: range (y + 1)
in
range 0
end
</code></pre>
</div>
</section>
<section id="slide-org1190219">
<h3 id="org1190219">Options</h3>
<aside class="notes">
<ul>
<li>Option is a container that holds a single value</li>
<li>We have isSome and valOf that work on options</li>
<li>isSome tells us if an Option is non-empty</li>
<li>valOf extracts values, or throws on NONE</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >val a: int option = SOME 5
val b: int option = NONE
isSome b = false
val c: int = if isSome a then valOf a else 0
</code></pre>
</div>
</section>
<section id="slide-orgb1b9ece">
<h3 id="orgb1b9ece">Options</h3>
<aside class="notes">
<ul>
<li>Options provide a safer mechanism than exception handling</li>
<li>Functions can expect to handle failure without exceptions or null</li>
<li>The first example here would throw an Empty exception if the
character can't be found</li>
<li>The second example won't throw, but uses a magic number as the
return value in its failure condition</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun strchr (s: char list, c: char, acc: int) =
if hd s = c
then acc
else strchr (tl s, c, acc + 1)
fun strchr' (s: char list, c: char, acc: int) =
if s = []
then ~1
else
if hd s = c
then acc
else strchr' (tl s, c, acc + 1)
</code></pre>
</div>
</section>
<section id="slide-org5bef4f3">
<h3 id="org5bef4f3">Options</h3>
<aside class="notes">
<ul>
<li>By using local bindings to create a private function, we get a
type-safe wrapper around an otherwise unsafe or inconsistent function</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun strchr (s: string, c: char) =
let
fun strchr' (s: char list, acc: int) =
if s = []
then ~1
else
if hd s = c
then acc
else strchr' (tl s, acc + 1)
val i = strchr' (explode s, 0)
in
if i = ~1 then NONE else SOME i
end
</code></pre>
</div>
</section>
<section id="slide-org668e650">
<h3 id="org668e650">Data Types</h3>
<aside class="notes">
<ul>
<li>Not only is there the Option type, ML lets us define our own custom types</li>
<li>The type is defined on the left</li>
<li>Various constructors for the type go on the right</li>
<li>Constructor can be "of" an existing type, or a singleton</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >datatype Toppings = Mustard of string
| Pickles of int
| PepperAndOnion of int * int
| Relish
</code></pre>
</div>
</section>
<section id="slide-orgf1d0cdc">
<h3 id="orgf1d0cdc">Data Types</h3>
<aside class="notes">
<ul>
<li>These types can then be used much like any other type</li>
<li>This includes in conjunction with container types like list and option</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >datatype HotDogStyle = ToppedWith of Toppings list
| Plain
datatype HotDog = Link of HotDogStyle
| Brat of HotDogStyle;
val myToppings:HotDogStyle = [Mustard("dijon"), Relish, Pickles(2)]
val myDog = Brat (ToppedWith myToppings)
val yourDog = SOME(Link Plain)
</code></pre>
</div>
</section>
<section id="slide-org11bcf96">
<h3 id="org11bcf96">Type Bindings</h3>
<aside class="notes">
<ul>
<li>In addition to data types, we have type bindings</li>
<li>Type bindings work as synonyms for existing types</li>
<li>Type bindings do not create new constructors</li>
<li>This is especially useful for records: records as params are typing hell</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >type hotdogOrder = int * HotDog
val myOrder = 2 * myDog
type attendee = { name : string,
company : string option,
experience : int }
fun attendeeName (a: attendee):string = #name a
attendeeName {name= "bob", company= NONE, experience= 2}
</code></pre>
</div>
</section>
<section id="slide-orgb102c91">
<h3 id="orgb102c91">Pattern Matching</h3>
<aside class="notes">
<ul>
<li>Case statements provide a way of "deconstructing" type constructors</li>
<li>Matches can be made on any type of constructor</li>
<li>this includes Option and list like we've seen already</li>
<li>Underscore is used as a wild card</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun likesBrats (d: HotDog) =
case d of
Brat _ => true
| Link _ => false
likesBrats (Brat Plain)
</code></pre>
</div>
</section>
<section id="slide-org8d6595f">
<h3 id="org8d6595f">Pattern Matching</h3>
<aside class="notes">
<ul>
<li>using local bindings can clean up nested cases</li>
<li>here 'style' is used to "break off" the top constructor to get
the inner values</li>
<li>here's a sneak-peak at lambda syntax</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun likesRelish (d: HotDog) =
let
val style = case d of
Brat s => s
| Link s => s
in
case style of
Plain => false
| ToppedWith ts => List.exists (fn t => t = Relish) ts
end
likesRelish (Link Plain) = false
likesRelish (Brat (ToppedWith [Relish])) = true
</code></pre>
</div>
</section>
<section id="slide-orgfdd9f46">
<h3 id="orgfdd9f46">Pattern Matching</h3>
<aside class="notes">
<ul>
<li>Again, matches can be made on any type of constructor</li>
<li>Case statements can be used to destructure basically any type</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun addTuple t =
case t of
(a, b, c) => a + b + c
fun sum l =
case l of
[] => 0
| h :: t => h + (sum t)
</code></pre>
</div>
</section>
<section id="slide-org3d48409">
<h3 id="org3d48409">Pattern Matching Function Signatures</h3>
<aside class="notes">
<ul>
<li>Functions have a special form of case expression</li>
<li>They can be written as multiple definitions instead</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun likesBrats (Brat _) = true
| likesBrats (Link _) = false
fun sum [] = 0
| sum (h :: t) = h + (sum t)
</code></pre>
</div>
</section>
<section id="slide-orgb42c769">
<h3 id="orgb42c769">Recursive & Polymorphic types</h3>
<aside class="notes">
<ul>
<li>Type variables are preceded with an apostrophe</li>
<li>They come before type name in data type definitions</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >datatype 'a Thing = Thing of 'a
(case Thing 1 of Thing n => n) = 1
(case Thing "Chimichanga" of Thing s => size s) = 11
datatype 'a lyzt = Emptee | Cawns of 'a * 'a lyzt
datatype ('a,'b) trie = Knowd of 'a * ('a,'b) trie * ('a,'b) trie
| Leef of 'b
</code></pre>
</div>
</section>
<section id="slide-orgb13e956">
<h3 id="orgb13e956">Tail Recursion & Accumulator Pattern</h3>
<aside class="notes">
<ul>
<li>Tail-call recursion optimization is of just called "tail recursion"</li>
<li>Return value of function is call to self</li>
<li>Can be optimized to not take up stack frames</li>
<li>No stack overflows works like a loop</li>
<li>Accumulator pattern provides a private function which builds the result set</li>
<li>This leaves an optimized function without sullying the API</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun sum l =
case l of
[] => 0
| h :: t => h + (sum t)
fun sum l =
let fun f(is, acc) =
case is of
[] => acc
| h :: t => f(t, h + acc)
in
f(l, 0)
end
</code></pre>
</div>
</section>
<section id="slide-org366caaa">
<h3 id="org366caaa">Higher Order & First Class Functions</h3>
<aside class="notes">
<ul>
<li>Higher order functions are functions that take other functions as
arguments</li>
<li>Functions in ML are first-class because they can be stored and
passed to functions as values, and returned from other functions</li>
<li>Functions can exist independently from definition as part of a
module as lambdas with the "fn" keyword.</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun repeat (f, n, x) = (* ('a -> 'a) * int * 'a -> 'a *)
if n = 0 then x else f (repeat(f, n - 1, x))
fun double x = x * 2 (* int -> int *)
repeat (double, 5, 2) = 64
repeat (fn x => x * x, 3, 2) = 256
val square = fn x => x * x (* int -> int *)
repeat (square, 3, 2) = 256
fun add y = fn x => x + y (* int -> int -> int *)
repeat (add 5, 10, 0) = 50
</code></pre>
</div>
</section>
<section id="slide-org2bd8cbc">
<h3 id="org2bd8cbc">Exercise time!</h3>
<aside class="notes">
<p>
fun map (f, xs) =
case xs of
[] => []
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">x :: xs' => (f x) :: map(f, xs'))</td>
</tr>
</tbody>
</table>
<p>
fun filter (f, xs) =
case xs of
[] => []
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">x :: xs => if f x then x :: filter(f, xs) else filter (f, xs)</td>
</tr>
</tbody>
</table>
</aside>
<ul>
<li>Define the function `map` to operate on list such that:</li>
</ul>
<div class="org-src-container">
<pre><code class="sml" >map (fn x => x + 1, [1,2,3,4,5]) = [2,3,4,5,6]
</code></pre>
</div>
<ul>
<li>Define the function `filter` to operate on lists such that:</li>
</ul>
<div class="org-src-container">
<pre><code class="sml" >filter (fn x => x mod 2 = 0, [1,2,3,4,5,6]) = [2,4,6]
</code></pre>
</div>
</section>
<section id="slide-org5050b2d">
<h3 id="org5050b2d">Folds</h3>
<aside class="notes">
<ul>
<li>Fold is a function that repeatedly applies a function to a
collection to accumulate a single result</li>
<li>folds are a common abstraction around the accumulator pattern we
just discussed</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun fold (f, acc, xs) =
case xs of
[] => acc
| x :: xs => fold (f, f(acc, x), xs)
fold(fn (x, y) => x + y, 0, [1,2,3,4,5]) = 15
fold(fn (x, y) => x * y, 1, [1,2,3,4,5]) = 120
</code></pre>
</div>
</section>
<section id="slide-orgf1efe00">
<h3 id="orgf1efe00">Function composition</h3>
<aside class="notes">
<ul>
<li>Function composition is creating a new function which executes
two functions serially</li>
<li>The compose function is in the standard library as the function
lowercase "o"</li>
</ul>
</aside>
<div class="org-src-container">
<pre><code class="sml" >fun compose (f, g) = fn x => f(g x)
fun add1 x = x + 1
fun times2 x = x * 2
(compose (times2, add1)) 2 = 6
(times2 o add1) 2 = 6
</code></pre>
</div>
</section>
<section id="slide-orgf0064cc">
<h3 id="orgf0064cc">The Pizza Operator</h3>
<aside class="notes">
<ul>
<li>Also known as the forward pipe</li>