forked from moocfi/haskell-mooc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.html
4925 lines (4923 loc) · 802 KB
/
part2.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 xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Haskell Mooc, part 2</title>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="./style.css">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<div id="everything">
<nav id="TOC">
<ul>
<li><a href="#lecture-9-recap-of-part-1"><span class="toc-section-number">9</span> Lecture 9: Recap of Part 1</a>
<ul>
<li><a href="#types"><span class="toc-section-number">9.1</span> Types</a></li>
<li><a href="#functions"><span class="toc-section-number">9.2</span> Functions</a></li>
<li><a href="#functional-programming"><span class="toc-section-number">9.3</span> Functional Programming</a></li>
<li><a href="#recursion"><span class="toc-section-number">9.4</span> Recursion</a></li>
<li><a href="#type-classes"><span class="toc-section-number">9.5</span> Type Classes</a></li>
<li><a href="#quiz"><span class="toc-section-number">9.6</span> Quiz</a></li>
<li><a href="#working-on-the-exercises"><span class="toc-section-number">9.7</span> Working on the Exercises</a></li>
<li><a href="#exercises"><span class="toc-section-number">9.8</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-10-reductionism"><span class="toc-section-number">10</span> Lecture 10: Reductionism</a>
<ul>
<li><a href="#laziness-purity"><span class="toc-section-number">10.1</span> Laziness & Purity</a></li>
<li><a href="#equational-reasoning"><span class="toc-section-number">10.2</span> Equational Reasoning</a></li>
<li><a href="#infinite-lists"><span class="toc-section-number">10.3</span> Infinite Lists</a></li>
<li><a href="#how-does-haskell-work"><span class="toc-section-number">10.4</span> How does Haskell Work?</a></li>
<li><a href="#working-with-infinite-lists"><span class="toc-section-number">10.5</span> Working with Infinite Lists</a></li>
<li><a href="#interlude-adding-strictness"><span class="toc-section-number">10.6</span> Interlude: Adding Strictness</a></li>
<li><a href="#newtype-declarations"><span class="toc-section-number">10.7</span> Newtype Declarations</a></li>
<li><a href="#something-fun-tying-the-knot"><span class="toc-section-number">10.8</span> Something Fun: Tying the Knot</a></li>
<li><a href="#something-fun-debug.trace"><span class="toc-section-number">10.9</span> Something Fun: Debug.Trace</a></li>
<li><a href="#quiz-1"><span class="toc-section-number">10.10</span> Quiz</a></li>
<li><a href="#exercises-1"><span class="toc-section-number">10.11</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-11-realworld---arealworld"><span class="toc-section-number">11</span> Lecture 11: <code>RealWorld -> (a,RealWorld)</code></a>
<ul>
<li><a href="#contents"><span class="toc-section-number">11.1</span> Contents</a></li>
<li><a href="#youve-been-fooled"><span class="toc-section-number">11.2</span> You’ve Been Fooled!</a></li>
<li><a href="#the-subtle-return"><span class="toc-section-number">11.3</span> The Subtle <code>return</code></a></li>
<li><a href="#do-and-types"><span class="toc-section-number">11.4</span> <code>do</code> and Types</a></li>
<li><a href="#control-structures"><span class="toc-section-number">11.5</span> Control Structures</a></li>
<li><a href="#a-word-about-do-and-indentation"><span class="toc-section-number">11.6</span> A Word About <code>do</code> and Indentation</a></li>
<li><a href="#lets-write-a-program"><span class="toc-section-number">11.7</span> Let’s Write a Program</a></li>
<li><a href="#what-does-it-all-mean"><span class="toc-section-number">11.8</span> What Does It All Mean?</a></li>
<li><a href="#one-more-thing-ioref"><span class="toc-section-number">11.9</span> One More Thing: IORef</a></li>
<li><a href="#summary-of-io"><span class="toc-section-number">11.10</span> Summary of IO</a></li>
<li><a href="#quiz-2"><span class="toc-section-number">11.11</span> Quiz</a></li>
<li><a href="#exercises-2"><span class="toc-section-number">11.12</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-12-fmap-fmap-fmap"><span class="toc-section-number">12</span> Lecture 12: fmap fmap fmap</a>
<ul>
<li><a href="#contents-1"><span class="toc-section-number">12.1</span> Contents</a></li>
<li><a href="#functors"><span class="toc-section-number">12.2</span> Functors</a></li>
<li><a href="#lawful-instances"><span class="toc-section-number">12.3</span> Lawful Instances</a></li>
<li><a href="#sidenote-kinds"><span class="toc-section-number">12.4</span> Sidenote: Kinds</a></li>
<li><a href="#foldable-again"><span class="toc-section-number">12.5</span> <code>Foldable</code>, Again</a></li>
<li><a href="#recap"><span class="toc-section-number">12.6</span> Recap</a></li>
<li><a href="#quiz-3"><span class="toc-section-number">12.7</span> Quiz</a></li>
<li><a href="#exercises-3"><span class="toc-section-number">12.8</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-13-a-monoid-in-the-category-of-problems"><span class="toc-section-number">13</span> Lecture 13: A Monoid in the Category of Problems</a>
<ul>
<li><a href="#example-1-maybes"><span class="toc-section-number">13.1</span> Example 1: Maybes</a></li>
<li><a href="#example-2-logging"><span class="toc-section-number">13.2</span> Example 2: Logging</a></li>
<li><a href="#example-3-keeping-state"><span class="toc-section-number">13.3</span> Example 3: Keeping State</a></li>
<li><a href="#finally-the-monad-type-class"><span class="toc-section-number">13.4</span> Finally: The Monad Type Class</a></li>
<li><a href="#maybe-is-a-monad"><span class="toc-section-number">13.5</span> Maybe is a Monad!</a></li>
<li><a href="#the-return-of-do"><span class="toc-section-number">13.6</span> The Return of <code>do</code></a></li>
<li><a href="#logger-is-a-monad"><span class="toc-section-number">13.7</span> Logger is a Monad!</a></li>
<li><a href="#the-state-monad"><span class="toc-section-number">13.8</span> The State Monad</a></li>
<li><a href="#the-return-of-mapm"><span class="toc-section-number">13.9</span> The Return of <code>mapM</code></a></li>
<li><a href="#monads-are-functors"><span class="toc-section-number">13.10</span> Monads are Functors</a></li>
<li><a href="#one-more-monad"><span class="toc-section-number">13.11</span> One More Monad</a></li>
<li><a href="#oh-right-io"><span class="toc-section-number">13.12</span> Oh Right, IO</a></li>
<li><a href="#monads-in-other-languages"><span class="toc-section-number">13.13</span> Monads in Other Languages</a></li>
<li><a href="#monads-wrap-up"><span class="toc-section-number">13.14</span> Monads: Wrap-up</a></li>
<li><a href="#sidenote-standard-haskell"><span class="toc-section-number">13.15</span> Sidenote: Standard Haskell</a></li>
<li><a href="#quiz-4"><span class="toc-section-number">13.16</span> Quiz</a></li>
<li><a href="#exercises-4"><span class="toc-section-number">13.17</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-14-lets-use-some-libraries"><span class="toc-section-number">14</span> Lecture 14: Let’s Use Some Libraries!</a>
<ul>
<li><a href="#text-and-bytestring"><span class="toc-section-number">14.1</span> <code>Text</code> and <code>ByteString</code></a></li>
<li><a href="#monads-recap"><span class="toc-section-number">14.2</span> Monads: Recap</a></li>
<li><a href="#writing-a-http-server-wai-and-warp"><span class="toc-section-number">14.3</span> Writing a HTTP Server: WAI and Warp</a></li>
<li><a href="#working-with-a-database-sqlite-simple"><span class="toc-section-number">14.4</span> Working With a Database: sqlite-simple</a></li>
<li><a href="#exercises-5"><span class="toc-section-number">14.5</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-15-youre-valid-even-without-monads"><span class="toc-section-number">15</span> Lecture 15: You’re Valid Even Without Monads</a>
<ul>
<li><a href="#introduction-to-applicatives"><span class="toc-section-number">15.1</span> Introduction to Applicatives</a></li>
<li><a href="#the-list-applicative"><span class="toc-section-number">15.2</span> The List Applicative</a></li>
<li><a href="#new-operators"><span class="toc-section-number">15.3</span> New Operators</a></li>
<li><a href="#the-validation-applicative"><span class="toc-section-number">15.4</span> The Validation Applicative</a></li>
<li><a href="#validating-lists-traverse"><span class="toc-section-number">15.5</span> Validating Lists: <code>traverse</code></a></li>
<li><a href="#sidenote-traversable"><span class="toc-section-number">15.6</span> Sidenote: <code>Traversable</code></a></li>
<li><a href="#dealing-with-failure-alternative"><span class="toc-section-number">15.7</span> Dealing with Failure: <code>Alternative</code></a></li>
<li><a href="#sidenote-applicatives-in-context"><span class="toc-section-number">15.8</span> Sidenote: Applicatives in Context</a></li>
<li><a href="#quiz-5"><span class="toc-section-number">15.9</span> Quiz</a></li>
<li><a href="#exercises-6"><span class="toc-section-number">15.10</span> Exercises</a></li>
</ul></li>
<li><a href="#lecture-16-odds-and-ends"><span class="toc-section-number">16</span> Lecture 16: Odds and Ends</a>
<ul>
<li><a href="#testing-with-quickcheck"><span class="toc-section-number">16.1</span> Testing with QuickCheck</a></li>
<li><a href="#phantom-types"><span class="toc-section-number">16.2</span> Phantom Types</a></li>
<li><a href="#simultaneity"><span class="toc-section-number">16.3</span> Simultaneity</a></li>
<li><a href="#exercises-7"><span class="toc-section-number">16.4</span> Exercises</a></li>
<li><a href="#where-to-go-from-here"><span class="toc-section-number">16.5</span> Where to Go From Here?</a></li>
<li><a href="#acknowledgements"><span class="toc-section-number">16.6</span> Acknowledgements</a></li>
</ul></li>
</ul>
</nav>
<div id="text-container">
<div id="text">
<!-- MENU -->
<header>
<h1 class="title">Haskell Mooc, part 2</h1>
</header>
<p>by Joel Kaasinen (<a href="https://nitor.com/en">Nitor</a>) and John Lång (University of Helsinki)</p>
<section id="lecture-9-recap-of-part-1" data-number="1">
<h1 data-number="9"><span class="header-section-number">9</span> Lecture 9: Recap of Part 1</h1>
<p>This lecture goes over the basic parts of Haskell introduced in part 1 of the course: types, values, pattern matching, functions and recursion.</p>
<section id="types" data-number="1.1">
<h2 data-number="9.1"><span class="header-section-number">9.1</span> Types</h2>
<p>Remember the primitive types of Haskell? Here they are:</p>
<table>
<colgroup>
<col style="width: 35%" />
<col style="width: 10%" />
<col style="width: 54%" />
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Values</th>
<th style="text-align: left;">Type</th>
<th style="text-align: left;">Meaning</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>True</code>, <code>False</code></td>
<td style="text-align: left;"><code>Bool</code></td>
<td style="text-align: left;">Truth values</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>0</code>, <code>1</code>, <code>20</code>, <code>-37</code>, …</td>
<td style="text-align: left;"><code>Int</code></td>
<td style="text-align: left;">Whole numbers</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>'A'</code>, <code>'a'</code>, <code>'!'</code>, …</td>
<td style="text-align: left;"><code>Char</code></td>
<td style="text-align: left;">Characters</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>""</code>, <code>"abcd"</code>, …</td>
<td style="text-align: left;"><code>String</code></td>
<td style="text-align: left;">Strings, which are actually just lists of characters, <code>[Char]</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>0.0</code>, <code>-3.2</code>, <code>12.3</code>, …</td>
<td style="text-align: left;"><code>Double</code></td>
<td style="text-align: left;">Floating-point numbers</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>()</code></td>
<td style="text-align: left;"><code>()</code></td>
<td style="text-align: left;">The so-called unit type with only one value</td>
</tr>
</tbody>
</table>
<p>It’s possible to combine these primitive types in various ways to form more complex types. Function types, tuple types and list types are examples of types that combine other types.</p>
<table>
<colgroup>
<col style="width: 41%" />
<col style="width: 12%" />
<col style="width: 45%" />
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Values</th>
<th style="text-align: left;">Type</th>
<th style="text-align: left;">Meaning</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>(1,2)</code>, <code>(True,'c')</code>, …</td>
<td style="text-align: left;"><code>(a, b)</code></td>
<td style="text-align: left;">A pair of a value of type <code>a</code> and a value of type <code>b</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>(1,2,3)</code>, <code>(1,2,'c')</code>, …</td>
<td style="text-align: left;"><code>(a, b, c)</code></td>
<td style="text-align: left;">A triple of values (of types <code>a</code>, <code>b</code> and <code>c</code>)</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>[]</code>, <code>[1,2,3,4]</code>, …</td>
<td style="text-align: left;"><code>[a]</code></td>
<td style="text-align: left;">List of values of type <code>a</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>not</code>, <code>reverse</code>, <code>\x -> 1</code>, <code>\x -> x</code>, …</td>
<td style="text-align: left;"><code>a -> b</code></td>
<td style="text-align: left;">Function from type <code>a</code> to type <code>b</code></td>
</tr>
</tbody>
</table>
<p>There’s one more powerful mechanism for creating more types: <em>Algebraic datatypes</em> (ADTs). Some examples include:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- Enumeration types</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Bool</span> <span class="ot">=</span> <span class="dt">True</span> <span class="op">|</span> <span class="dt">False</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Color</span> <span class="ot">=</span> <span class="dt">Red</span> <span class="op">|</span> <span class="dt">Green</span> <span class="op">|</span> <span class="dt">Blue</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co">-- Record types that contain fields</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Vector2d</span> <span class="ot">=</span> <span class="dt">MakeVector</span> <span class="dt">Double</span> <span class="dt">Double</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">Person</span> <span class="dt">Int</span> <span class="dt">String</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- Parameterized types. Note the type parameter `a`</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">PairOf</span> a <span class="ot">=</span> <span class="dt">TwoValues</span> a a</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co">-- Recursive types</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">IntList</span> <span class="ot">=</span> <span class="dt">Empty</span> <span class="op">|</span> <span class="dt">Node</span> <span class="dt">Int</span> <span class="dt">IntList</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a><span class="co">-- Complex types which combine many of these features</span></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Maybe</span> a <span class="ot">=</span> <span class="dt">Nothing</span> <span class="op">|</span> <span class="dt">Just</span> a</span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Either</span> a b <span class="ot">=</span> <span class="dt">Left</span> a <span class="op">|</span> <span class="dt">Right</span> b</span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">List</span> a <span class="ot">=</span> <span class="dt">Nil</span> <span class="op">|</span> <span class="dt">Cons</span> a (<span class="dt">List</span> a) <span class="co">-- This is equivalent to the built-in [a] type</span></span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tree</span> a <span class="ot">=</span> <span class="dt">Leaf</span> a <span class="op">|</span> <span class="dt">Node</span> a (<span class="dt">Tree</span> a) (<span class="dt">Tree</span> a)</span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">MultiTree</span> a <span class="ot">=</span> <span class="dt">MultiTree</span> a [<span class="dt">MultiTree</span> a] <span class="co">-- Note the list</span></span></code></pre></div>
<p>Values of these types include:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: left;">Values</th>
<th style="text-align: left;">Type</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>True</code>, <code>False</code></td>
<td style="text-align: left;"><code>Bool</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Red</code>, <code>Green</code>, <code>Blue</code></td>
<td style="text-align: left;"><code>Color</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>MakeVector 1.5 3.2</code></td>
<td style="text-align: left;"><code>Vector2d</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Person 13 "Bob"</code></td>
<td style="text-align: left;"><code>Person</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>TwoValues 1 3</code></td>
<td style="text-align: left;"><code>PairOf Int</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Empty</code>, <code>Node 3 (Node 4 Empty)</code></td>
<td style="text-align: left;"><code>IntList</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Nothing</code>, <code>Just 3</code>, <code>Just 4</code>, …</td>
<td style="text-align: left;"><code>Maybe Int</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Nothing</code>, <code>Just 'c'</code>, <code>Just 'd'</code>, …</td>
<td style="text-align: left;"><code>Maybe Char</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Left "foo"</code>, <code>Right 13</code>, …</td>
<td style="text-align: left;"><code>Either String Int</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Nil</code>, <code>Cons True Nil</code>, <code>Cons True (Cons False Nil)</code> …</td>
<td style="text-align: left;"><code>List Bool</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Leaf 7</code>, <code>Node 1 (Leaf 0) (Leaf 2)</code>, …</td>
<td style="text-align: left;"><code>Tree Int</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>MultiTree 'a' [MultiTree 'b' [], MultiTree 'c' []]]</code>, …</td>
<td style="text-align: left;"><code>MultiTree Char</code></td>
</tr>
</tbody>
</table>
<p>You can combine parameterized types in complex ways, for example with something like <code>Either [String->String] (Maybe String, Int)</code>.</p>
<p>The names of concrete types start with capital letters. Lowercase letters are used for <em>type variables</em> which indicate <em>parametric polymorphism</em>: functions and values that can have multiple types. Here are some examples of polymorphic function types:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>[a] <span class="ot">-></span> [a] <span class="co">-- function from list of any type, to list of the same type</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>[a] <span class="ot">-></span> a <span class="co">-- function from list of any type, to the element type</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>(a,b) <span class="ot">-></span> [a] <span class="co">-- function from tuple to list</span></span></code></pre></div>
<section id="more-about-lists" data-number="1.1.1">
<h3 data-number="9.1.1"><span class="header-section-number">9.1.1</span> More About Lists</h3>
<p>List literals can be written in using the familiar <code>[x,y,z]</code> syntax. However, that notation is just a shorthand as lists are actually built up of the list constructors <code>[]</code> and <code>(:)</code>. These constructors are also used when pattern matching lists. Here are some examples of lists:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: left;">Abbreviation</th>
<th style="text-align: left;">Full list</th>
<th style="text-align: left;">Type</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>[1,2,3]</code></td>
<td style="text-align: left;"><code>1:2:3:[]</code></td>
<td style="text-align: left;"><code>[Int]</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>[[1],[2],[3]]</code></td>
<td style="text-align: left;"><code>(1:[]):(2:[]):(3:[]):[]</code></td>
<td style="text-align: left;"><code>[[Int]]</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>"foo"</code></td>
<td style="text-align: left;"><code>'f':'o':'o':[]</code></td>
<td style="text-align: left;"><code>[Char]</code>, also known as <code>String</code></td>
</tr>
</tbody>
</table>
<p>There’s also a range syntax for lists:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: left;">Range</th>
<th style="text-align: left;">Result</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>['a' .. 'z']</code></td>
<td style="text-align: left;"><code>"abcdefghijklmnopqrstuvwxyz"</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>[0 .. 9]</code></td>
<td style="text-align: left;"><code>[0,1,2,3,4,5,6,7,8,9]</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>[0, 5 .. 25]</code></td>
<td style="text-align: left;"><code>[0,5,10,15,20,25]</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>[x .. y]</code></td>
<td style="text-align: left;">everything from <code>x</code> to <code>y</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>[9 .. 3]</code></td>
<td style="text-align: left;"><code>[]</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>[y, y-1 .. x]</code></td>
<td style="text-align: left;">everything from <code>y</code> to <code>x</code> in decreasing order</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>[9,8 .. 3]</code></td>
<td style="text-align: left;"><code>[9,8,7,6,5,4,3]</code></td>
</tr>
</tbody>
</table>
<p><em>List comprehensions</em> are another powerful way to create lists:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: left;">Comprehension</th>
<th style="text-align: left;">Result</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>[x^3 | x <- [1..3]]</code></td>
<td style="text-align: left;"><code>[1,8,27]</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>[x^2 + y^2 | x <- [1..3], y <- [1..2]]</code></td>
<td style="text-align: left;"><code>[2,5,5,8,10,13]</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>[y | x <- [1..10], let y = x^2, even x, y<50]</code></td>
<td style="text-align: left;"><code>[4,16,36]</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>[c | c <- "Hello, World!", elem c ['a'..'z']]</code></td>
<td style="text-align: left;"><code>"elloorld"</code></td>
</tr>
</tbody>
</table>
<p>In general, <code>[f x | x <- xs, p x]</code> is the same as <code>map f (filter p xs)</code>. Also, <code>[y | x <- xs, let y = f x]</code> is the same as <code>[f x | x <- xs]</code>. Any combination of <code><-</code>, <code>let</code> and <code>[f x | ...]</code> is possible.</p>
<p>Just one more note on the syntax. Recall that <code>(:)</code> associates to the right, e.g. <code>True:False:[]</code> is the same as <code>True:(False:[])</code>. (In fact, <code>(True:False):[]</code> is not even a list, because <code>True:False</code> attempts to add <code>True</code> in front of <code>False</code> which is not a list.)</p>
</section>
</section>
<section id="functions" data-number="1.2">
<h2 data-number="9.2"><span class="header-section-number">9.2</span> Functions</h2>
<p>The basic form of function definition is:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ot">functionName ::</span> argumentType <span class="ot">-></span> returnType</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>functionName argument <span class="ot">=</span> returnValue</span></code></pre></div>
<p>For example:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ot">repeatString ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">String</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>repeatString s <span class="ot">=</span> s <span class="op">++</span> s</span></code></pre></div>
<p>Functions taking multiple arguments are defined in a similar manner. Note how the type of a multi-argument function looks like.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">surroundString ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">String</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>surroundString around s <span class="ot">=</span> around <span class="op">++</span> s <span class="op">++</span> around</span></code></pre></div>
<p>Functions can be polymorphic, can take multiple arguments, and can even take functions as arguments. Here are more examples:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">id</span><span class="ot"> ::</span> a <span class="ot">-></span> a</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="fu">id</span> x <span class="ot">=</span> x</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="fu">const</span><span class="ot"> ::</span> a <span class="ot">-></span> b <span class="ot">-></span> a</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="fu">const</span> x y <span class="ot">=</span> x</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="fu">flip</span><span class="ot"> ::</span> (a <span class="ot">-></span> b <span class="ot">-></span> c) <span class="ot">-></span> b <span class="ot">-></span> a <span class="ot">-></span> c</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="fu">flip</span> f x y <span class="ot">=</span> f y x</span></code></pre></div>
<p>More sophisticated functions can be defined using <em>pattern matching</em>. We can pattern match on the <em>constructors</em> of algebraic datatypes like <code>Maybe</code>, and also lists with the constructors <code>[]</code> and <code>(:)</code>.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ot">swap ::</span> (a,b) <span class="ot">-></span> (b,a)</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>swap (x,y) <span class="ot">=</span> (y,x)</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="fu">maybe</span><span class="ot"> ::</span> b <span class="ot">-></span> (a <span class="ot">-></span> b) <span class="ot">-></span> <span class="dt">Maybe</span> a <span class="ot">-></span> b</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="fu">maybe</span> def _ <span class="dt">Nothing</span> <span class="ot">=</span> def</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a><span class="fu">maybe</span> _ f (<span class="dt">Just</span> x) <span class="ot">=</span> f x</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="ot">safeHead ::</span> [a] <span class="ot">-></span> <span class="dt">Maybe</span> a</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>safeHead [] <span class="ot">=</span> <span class="dt">Nothing</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a>safeHead (x<span class="op">:</span>_) <span class="ot">=</span> <span class="dt">Just</span> x</span></code></pre></div>
<p>Yet more sophistication can be achieved with <em>guards</em>. Guards let you define a function case-by-case based on tests of type <code>Bool</code>. Guards are useful in situations where pattern matching can’t be used. Of course, guards can also be combined with pattern matching:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ot">myAbs ::</span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>myAbs x</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> x <span class="op"><</span> <span class="dv">0</span> <span class="ot">=</span> <span class="op">-</span>x</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> x</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a><span class="ot">safeDiv ::</span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Maybe</span> <span class="dt">Double</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>safeDiv x y</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> y <span class="op">==</span> <span class="dv">0</span> <span class="ot">=</span> <span class="dt">Nothing</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> <span class="dt">Just</span> (x <span class="op">/</span> y)</span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a><span class="ot">buy ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">String</span></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>buy <span class="st">"Banana"</span> money</span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> money <span class="op"><</span> <span class="fl">3.2</span> <span class="ot">=</span> <span class="st">"You don't have enough money for a banana"</span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> <span class="st">"You bought a banana"</span></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>buy <span class="fu">product</span> _ <span class="ot">=</span> <span class="st">"No such product: "</span> <span class="op">++</span> <span class="fu">product</span></span></code></pre></div>
<p><em>Case expressions</em> let us pattern match inside functions. They are useful in situations where the result of one function depends on the result of another and we want to match the pattern on the output of the other function:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="ot">divDefault ::</span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Double</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>divDefault x y def <span class="ot">=</span> <span class="kw">case</span> safeDiv x y <span class="kw">of</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">Nothing</span> <span class="ot">-></span> def</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">Just</span> w <span class="ot">-></span> w</span></code></pre></div>
<p>Let-expressions enable <em>local definitions</em>. Where-clauses work similarly to <code>let</code>s. For example:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ot">circleArea ::</span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Double</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>circleArea r <span class="ot">=</span> <span class="kw">let</span> <span class="fu">pi</span> <span class="ot">=</span> <span class="fl">3.1415926</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> square x <span class="ot">=</span> x <span class="op">*</span> x</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">in</span> <span class="fu">pi</span> <span class="op">*</span> square r</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span class="ot">circleArea' ::</span> <span class="dt">Double</span> <span class="ot">-></span> <span class="dt">Double</span></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a>circleArea' r <span class="ot">=</span> <span class="fu">pi</span> <span class="op">*</span> square r</span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span> <span class="fu">pi</span> <span class="ot">=</span> <span class="fl">3.1415926</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> square x <span class="ot">=</span> x <span class="op">*</span> x</span></code></pre></div>
<p><em>Lambda expressions</em> are another occasionally useful syntax for defining functions. Lambda expressions represent anonymous (unnamed) functions. They can be used for defining local functions that are typically used only once.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="ot">incrementAll ::</span> [<span class="dt">Int</span>] <span class="ot">-></span> [<span class="dt">Int</span>]</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>incrementAll xs <span class="ot">=</span> <span class="fu">map</span> (\x <span class="ot">-></span> x <span class="op">+</span> <span class="dv">1</span>) xs</span></code></pre></div>
<p>Note that <code>f x = y</code> is the same thing as <code>f = \x -> y</code>.</p>
<p>Finally, binary operators have <em>sections</em>. Sections are partially applied operators. The section of an operator is obtained by writing the operator and one of its arguments in parentheses. For example, <code>(*2)</code> multiplies its argument by <code>2</code> from the right, e.g. <code>(*2) 5 ==> 5 * 2</code>. A fractional number (e.g. a <code>Double</code>) can be inverted with the section <code>(1/)</code>, e.g. <code>(1/) 2 ==> 0.5</code>.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">incrementAll' ::</span> [<span class="dt">Int</span>] <span class="ot">-></span> [<span class="dt">Int</span>]</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>incrementAll' xs <span class="ot">=</span> <span class="fu">map</span> (<span class="op">+</span><span class="dv">1</span>) xs</span></code></pre></div>
</section>
<section id="functional-programming" data-number="1.3">
<h2 data-number="9.3"><span class="header-section-number">9.3</span> Functional Programming</h2>
<p>Haskell is a functional programming language, which means that functions can be passed in as arguments and returned from functions. As a programming paradigm, functional programming aims to build programs by combining simple functions together to form larger and larger ones.</p>
<p>The most often presented example of functional programming is functional list manipulation using <em>higher-order functions</em> (functions that take functions as arguments) like <code>map</code> and <code>filter</code>. Here’s one example from part 1:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- a predicate that checks if a string is a palindrome</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="ot">palindrome ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">Bool</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>palindrome str <span class="ot">=</span> str <span class="op">==</span> <span class="fu">reverse</span> str</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="co">-- palindromes n takes all numbers from 1 to n, converts them to</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a><span class="co">-- strings using show, and keeps only palindromes</span></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a><span class="ot">palindromes ::</span> <span class="dt">Int</span> <span class="ot">-></span> [<span class="dt">String</span>]</span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a>palindromes n <span class="ot">=</span> <span class="fu">filter</span> palindrome (<span class="fu">map</span> <span class="fu">show</span> [<span class="dv">1</span><span class="op">..</span>n])</span></code></pre></div>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>palindromes <span class="dv">150</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> [<span class="st">"1"</span>,<span class="st">"2"</span>,<span class="st">"3"</span>,<span class="st">"4"</span>,<span class="st">"5"</span>,<span class="st">"6"</span>,<span class="st">"7"</span>,<span class="st">"8"</span>,<span class="st">"9"</span>,</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"11"</span>,<span class="st">"22"</span>,<span class="st">"33"</span>,<span class="st">"44"</span>,<span class="st">"55"</span>,<span class="st">"66"</span>,<span class="st">"77"</span>,<span class="st">"88"</span>,<span class="st">"99"</span>,</span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"101"</span>,<span class="st">"111"</span>,<span class="st">"121"</span>,<span class="st">"131"</span>,<span class="st">"141"</span>]</span></code></pre></div>
<p>We also encountered other functional programming patterns in part 1, like <em>partial application</em>:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> (<span class="fu">take</span> <span class="dv">3</span>) [[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>],[<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">0</span>]]</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> [[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>],[<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>]]</span></code></pre></div>
<p>Also, <em>function composition</em>:</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>(<span class="fu">map</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">filter</span> (<span class="op">/=</span><span class="st">"Smith"</span>)) [<span class="st">"Jones"</span>,<span class="st">"Smith"</span>,<span class="st">"White"</span>]</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> [<span class="st">"senoJ"</span>,<span class="st">"etihW"</span>]</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> (<span class="fu">negate</span> <span class="op">.</span> <span class="fu">sum</span>) [[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>],[<span class="dv">5</span>]]</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> [<span class="op">-</span><span class="dv">6</span>,<span class="op">-</span><span class="dv">5</span>]</span></code></pre></div>
<p>And finally, <em>folds</em>:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">foldr</span> (<span class="op">*</span>) <span class="dv">1</span> [<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>] <span class="op">==></span> <span class="dv">24</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="fu">foldr</span> <span class="fu">max</span> <span class="dv">0</span> [<span class="dv">1</span>,<span class="dv">3</span>,<span class="dv">7</span>] <span class="op">==></span> <span class="dv">7</span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a><span class="fu">foldr</span> (<span class="op">++</span>) <span class="st">""</span> [<span class="st">"abc"</span>,<span class="st">"de"</span>,<span class="st">"f"</span>] <span class="op">==></span> <span class="st">"abcdef"</span></span></code></pre></div>
</section>
<section id="recursion" data-number="1.4">
<h2 data-number="9.4"><span class="header-section-number">9.4</span> Recursion</h2>
<p>To implement a function that uses repetition in Haskell, you need recursion. Haskell has no loops like other programming languages. Here are some simple recursive functions in Haskell:</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ot">repeatString ::</span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">String</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>repeatString <span class="dv">0</span> s <span class="ot">=</span> <span class="st">""</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>repeatString n s <span class="ot">=</span> s <span class="op">++</span> repeatString (n<span class="op">-</span><span class="dv">1</span>) s</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a><span class="ot">times ::</span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span></span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a>times <span class="dv">0</span> n <span class="ot">=</span> <span class="dv">0</span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a>times <span class="dv">1</span> n <span class="ot">=</span> n</span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a>times m n <span class="ot">=</span> n <span class="op">+</span> times (m <span class="op">-</span> <span class="dv">1</span>) n</span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a><span class="ot">safeLast ::</span> [a] <span class="ot">-></span> <span class="dt">Maybe</span> a</span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a>safeLast [] <span class="ot">=</span> <span class="dt">Nothing</span></span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a>safeLast [x] <span class="ot">=</span> <span class="dt">Just</span> x</span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a>safeLast (x<span class="op">:</span>xs) <span class="ot">=</span> safeLast xs</span></code></pre></div>
<p>To consume or produce a list you often need recursion. Here are the implementations of <code>map</code> and <code>filter</code> as examples of recursive list processing:</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span><span class="ot"> ::</span> (a <span class="ot">-></span> b) <span class="ot">-></span> [a] <span class="ot">-></span> [b]</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> _ [] <span class="ot">=</span> []</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> f (x<span class="op">:</span>xs) <span class="ot">=</span> f x <span class="op">:</span> <span class="fu">map</span> f xs</span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="fu">filter</span><span class="ot"> ::</span> (a <span class="ot">-></span> <span class="dt">Bool</span>) <span class="ot">-></span> [a] <span class="ot">-></span> [a]</span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a><span class="fu">filter</span> _ [] <span class="ot">=</span> []</span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a><span class="fu">filter</span> <span class="fu">pred</span> (x<span class="op">:</span>xs)</span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">pred</span> x <span class="ot">=</span> x <span class="op">:</span> <span class="fu">filter</span> <span class="fu">pred</span> xs</span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> <span class="fu">filter</span> <span class="fu">pred</span> xs</span></code></pre></div>
<p>Sometimes, a recursive helper function is needed in case you need to keep track of multiple pieces of data.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="ot">sumNumbers ::</span> [<span class="dt">Int</span>] <span class="ot">-></span> <span class="dt">Int</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>sumNumbers xs <span class="ot">=</span> go <span class="dv">0</span> xs</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span> go <span class="fu">sum</span> [] <span class="ot">=</span> <span class="fu">sum</span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> go <span class="fu">sum</span> (x<span class="op">:</span>xs) <span class="ot">=</span> go (<span class="fu">sum</span><span class="op">+</span>x) xs</span></code></pre></div>
<p>Here’s a final example utilizing guards, pattern matching, a helper function and recursion:</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- split a string into pieces at the given character</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a><span class="ot">mySplit ::</span> <span class="dt">Char</span> <span class="ot">-></span> <span class="dt">String</span> <span class="ot">-></span> [<span class="dt">String</span>]</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>mySplit c xs <span class="ot">=</span> helper [] xs</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span> helper piece [] <span class="ot">=</span> [piece]</span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> helper piece (y<span class="op">:</span>ys)</span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> c <span class="op">==</span> y <span class="ot">=</span> piece <span class="op">:</span> helper [] ys</span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> helper (piece<span class="op">++</span>[y]) ys</span></code></pre></div>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>mySplit <span class="ch">'-'</span> <span class="st">"a-bcd-ef"</span> <span class="op">==></span> [<span class="st">"a"</span>,<span class="st">"bcd"</span>,<span class="st">"ef"</span>]</span></code></pre></div>
</section>
<section id="type-classes" data-number="1.5">
<h2 data-number="9.5"><span class="header-section-number">9.5</span> Type Classes</h2>
<p>The following functions are <em>parametrically polymorphic</em>:</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">id</span><span class="ot"> ::</span> a <span class="ot">-></span> a</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="fu">id</span> x <span class="ot">=</span> x</span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span><span class="ot"> ::</span> [a] <span class="ot">-></span> a</span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span> (x<span class="op">:</span>_) <span class="ot">=</span> x</span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="fu">fst</span><span class="ot"> ::</span> (a,b) <span class="ot">-></span> a</span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a><span class="fu">fst</span> (x,y) <span class="ot">=</span> x</span></code></pre></div>
<p>Parametrically polymorphic functions always work the same way, no matter what types we’re working with. This means that we can’t define a special implementation of <code>id</code> just for the <code>Int</code> type, or <code>fst</code> for the type <code>(Bool, String)</code>.</p>
<p>By contrast, <em>ad hoc polymorphism</em> allows different types to have different implementations of the same function. Ad hoc polymorphism in Haskell can be achieved by defining a <em>type class</em> and then declaring <em>instances</em> of that type class for various types. Ad hoc polymorphism is a handy way for expressing a common set of operations, even when the implementation of the operations depends on the type they’re acting on.</p>
<p>Functions that use ad hoc polymorphism have a <em>class constraint</em> in their types. Here are some examples:</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">negate</span><span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="ot">(==) ::</span> <span class="dt">Eq</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="ot">-></span> <span class="dt">Bool</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="fu">sort</span><span class="ot"> ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a]</span></code></pre></div>
<p>A type like <code>Num a => a -> a</code> means: for any type <code>X</code> that’s a member of the <code>Num</code> class, this function has type <code>X -> X</code>. In other words, we can invoke <code>negate</code> on any number type, but not on other types:</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">negate</span> <span class="dv">1</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="op">-</span><span class="dv">1</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">negate</span> <span class="fl">1.0</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="op">-</span><span class="fl">1.0</span></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">negate</span> <span class="dt">True</span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a><span class="op"><</span>interactive<span class="op">>:</span><span class="dv">3</span><span class="op">:</span><span class="dv">1</span><span class="op">:</span> <span class="fu">error</span><span class="op">:</span></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a> • <span class="dt">No</span> <span class="kw">instance</span> for (<span class="dt">Num</span> <span class="dt">Bool</span>) arising from a use <span class="kw">of</span> ‘negate’</span></code></pre></div>
<p>Here’s a summary of some useful type classes from the standard library.</p>
<ul>
<li>Comparison
<ul>
<li><code>Eq</code> is for equality comparison. It contains the <code>==</code> operator</li>
<li><code>Ord</code> is for order comparison. It contains the ordered comparison operators like <code><</code> and <code>=></code>, and functions like <code>max</code> and <code>min</code>.</li>
</ul></li>
<li>Numbers
<ul>
<li><code>Num</code> is for all number types. It contains <code>+</code>, <code>-</code>, <code>*</code> and <code>negate</code>.</li>
<li><code>Integral</code> is for whole number types. Most notably, it contains integer division <code>div</code>.</li>
<li><code>Fractional</code> is for number types that support division, <code>/</code></li>
</ul></li>
<li>Converting values to strings
<ul>
<li><code>Show</code> contains the function <code>show :: Show a => a -> String</code> that converts values to strings</li>
<li><code>Read</code> contains the function <code>read :: Read a => String -> a</code> that is the inverse of <code>show</code></li>
</ul></li>
</ul>
<p>Sometimes, multiple class constraints are needed. For instance here:</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="ot">sumTwoSmallest ::</span> (<span class="dt">Num</span> a, <span class="dt">Ord</span> a) <span class="ot">=></span> [a] <span class="ot">-></span> a</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a>sumTwoSmallest xs <span class="ot">=</span> <span class="kw">let</span> (a<span class="op">:</span>b<span class="op">:</span>_) <span class="ot">=</span> <span class="fu">sort</span> xs</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">in</span> a<span class="op">+</span>b</span></code></pre></div>
<p>Now that we’ve seen some classes and types, let’s look at the syntax of declaring classes and instances. Here are two class definitions:</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> <span class="dt">Sized</span> a <span class="kw">where</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="ot"> empty ::</span> a <span class="co">-- a thing with size 0</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a><span class="ot"> size ::</span> a <span class="ot">-></span> <span class="dt">Int</span></span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> <span class="dt">Eq</span> a <span class="kw">where</span></span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a><span class="ot"> (==) ::</span> a <span class="ot">-></span> a <span class="ot">-></span> <span class="dt">Bool</span></span></code></pre></div>
<p>Consider the following data structures:</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Numbers</span> <span class="ot">=</span> <span class="dt">None</span> <span class="op">|</span> <span class="dt">One</span> <span class="dt">Int</span> <span class="op">|</span> <span class="dt">Two</span> <span class="dt">Int</span> <span class="dt">Int</span></span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">IntList</span> <span class="ot">=</span> <span class="dt">Nil</span> <span class="op">|</span> <span class="dt">ListNode</span> <span class="dt">Int</span> <span class="dt">IntList</span></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tree</span> a <span class="ot">=</span> <span class="dt">Leaf</span> <span class="op">|</span> <span class="dt">Node</span> a (<span class="dt">Tree</span> a) (<span class="dt">Tree</span> a)</span></code></pre></div>
<p>All of these have sizes that we can count, but we need to perform the operation differently:</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Sized</span> <span class="dt">Numbers</span> <span class="kw">where</span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> empty <span class="ot">=</span> <span class="dt">None</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a> size <span class="dt">None</span> <span class="ot">=</span> <span class="dv">0</span></span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a> size (<span class="dt">One</span> _) <span class="ot">=</span> <span class="dv">1</span></span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a> size (<span class="dt">Two</span> _ _) <span class="ot">=</span> <span class="dv">2</span></span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Sized</span> <span class="dt">IntList</span> <span class="kw">where</span></span>
<span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a> empty <span class="ot">=</span> <span class="dt">Nil</span></span>
<span id="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></a> size <span class="dt">Nil</span> <span class="ot">=</span> <span class="dv">0</span></span>
<span id="cb29-10"><a href="#cb29-10" aria-hidden="true" tabindex="-1"></a> size (<span class="dt">ListNode</span> _ list) <span class="ot">=</span> <span class="dv">1</span> <span class="op">+</span> size list</span>
<span id="cb29-11"><a href="#cb29-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-12"><a href="#cb29-12" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Sized</span> (<span class="dt">Tree</span> a) <span class="kw">where</span></span>
<span id="cb29-13"><a href="#cb29-13" aria-hidden="true" tabindex="-1"></a> empty <span class="ot">=</span> <span class="dt">Leaf</span></span>
<span id="cb29-14"><a href="#cb29-14" aria-hidden="true" tabindex="-1"></a> size <span class="dt">Leaf</span> <span class="ot">=</span> <span class="dv">0</span></span>
<span id="cb29-15"><a href="#cb29-15" aria-hidden="true" tabindex="-1"></a> size (<span class="dt">Node</span> _ left right) <span class="ot">=</span> <span class="dv">1</span> <span class="op">+</span> size left <span class="op">+</span> size right</span></code></pre></div>
<p>We can also easily declare <code>Eq</code> instances for <code>Numbers</code> and <code>IntList</code>:</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Eq</span> <span class="dt">Numbers</span> <span class="kw">where</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <span class="dt">None</span> <span class="op">==</span> <span class="dt">None</span> <span class="ot">=</span> <span class="dt">True</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> (<span class="dt">One</span> x) <span class="op">==</span> (<span class="dt">One</span> y) <span class="ot">=</span> x<span class="op">==</span>y</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> (<span class="dt">Two</span> x y) <span class="op">==</span> (<span class="dt">Two</span> z w) <span class="ot">=</span> x<span class="op">==</span>z <span class="op">&&</span> y<span class="op">==</span>w</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a> _ <span class="op">==</span> _ <span class="ot">=</span> <span class="dt">False</span> <span class="co">-- to handle cases like None == One 1</span></span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Eq</span> <span class="dt">IntList</span> <span class="kw">where</span></span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a> <span class="dt">Nil</span> <span class="op">==</span> <span class="dt">Nil</span> <span class="ot">=</span> <span class="dt">True</span></span>
<span id="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a> (<span class="dt">ListNode</span> x xs) <span class="op">==</span> (<span class="dt">ListNode</span> y ys) <span class="ot">=</span> x <span class="op">==</span> y <span class="op">&&</span> xs <span class="op">==</span> ys</span>
<span id="cb30-10"><a href="#cb30-10" aria-hidden="true" tabindex="-1"></a> _ <span class="op">==</span> _ <span class="ot">=</span> <span class="dt">False</span></span></code></pre></div>
<p>However, since the <code>Tree</code> datatype is parameterized over the element type <code>a</code>, we need an <code>Eq a</code> instance in order to have an <code>Eq (Tree a)</code> instance. This is achieved by adding a class constraint to the instance declaration. This is called an <em>instance hierarchy</em>.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Eq</span> a <span class="ot">=></span> <span class="dt">Eq</span> (<span class="dt">Tree</span> a) <span class="kw">where</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a> <span class="dt">Leaf</span> <span class="op">==</span> <span class="dt">Leaf</span> <span class="ot">=</span> <span class="dt">True</span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> (<span class="dt">Node</span> x l r) <span class="op">==</span> (<span class="dt">Node</span> x' l' r') <span class="ot">=</span> x <span class="op">==</span> x' <span class="op">&&</span> l <span class="op">==</span> l' <span class="op">&&</span> r <span class="op">==</span> r'</span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> _ <span class="op">==</span> _ <span class="ot">=</span> <span class="dt">False</span></span></code></pre></div>
<section id="deriving" data-number="1.5.1">
<h3 data-number="9.5.1"><span class="header-section-number">9.5.1</span> Deriving</h3>
<p>Some standard type classes, most notably <code>Show</code>, <code>Read</code>, <code>Eq</code> and <code>Ord</code> can be <em>derived</em>, that is, you can ask the compiler to generate automatic instances for you. For example we could have derived all of these classes for our earlier <code>Numbers</code> example.</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Numbers</span> <span class="ot">=</span> <span class="dt">None</span> <span class="op">|</span> <span class="dt">One</span> <span class="dt">Int</span> <span class="op">|</span> <span class="dt">Two</span> <span class="dt">Int</span> <span class="dt">Int</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Show</span>, <span class="dt">Read</span>, <span class="dt">Eq</span>, <span class="dt">Ord</span>)</span></code></pre></div>
<div class="sourceCode" id="cb33"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="dt">None</span> <span class="op">==</span> <span class="dt">One</span> <span class="dv">1</span> <span class="op">==></span> <span class="dt">False</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="dt">Two</span> <span class="dv">1</span> <span class="dv">2</span> <span class="op">==</span> <span class="dt">Two</span> <span class="dv">1</span> <span class="dv">2</span> <span class="op">==></span> <span class="dt">True</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a><span class="dt">None</span> <span class="op"><</span> <span class="dt">Two</span> <span class="dv">1</span> <span class="dv">2</span> <span class="op">==></span> <span class="dt">True</span></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a><span class="dt">Two</span> <span class="dv">1</span> <span class="dv">3</span> <span class="op"><</span> <span class="dt">Two</span> <span class="dv">1</span> <span class="dv">2</span> <span class="op">==></span> <span class="dt">False</span></span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a><span class="fu">show</span> (<span class="dt">Two</span> <span class="dv">1</span> <span class="dv">3</span>) <span class="op">==></span> <span class="st">"Two 1 3"</span></span></code></pre></div>
</section>
</section>
<section id="quiz" data-number="1.6">
<h2 data-number="9.6"><span class="header-section-number">9.6</span> Quiz</h2>
<p>What is the type of <code>('c',not)</code></p>
<ol class="quiz">
<li>
<code>[Char]</code>
</li>
<li>
<code>[Bool]</code>
</li>
<li class="correct">
<code>(Char,Bool -> Bool)</code>
</li>
<li>
<code>(Char,Bool)</code>
</li>
<li>
It is a type error.
</li>
</ol>
<p>What is the type of <code>['c',not]</code></p>
<ol class="quiz">
<li>
<code>[Char]</code>
</li>
<li>
<code>[Bool]</code>
</li>
<li>
<code>(Char,Bool -> Bool)</code>
</li>
<li>
<code>(Char,Bool)</code>
</li>
<li class="correct">
It is a type error.
</li>
</ol>
<p>Which of these is a value of the following type?</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">T</span> <span class="ot">=</span> <span class="dt">X</span> <span class="dt">Int</span> <span class="op">|</span> <span class="dt">Y</span> <span class="dt">String</span> <span class="dt">String</span> <span class="op">|</span> <span class="dt">Z</span> <span class="dt">T</span></span></code></pre></div>
<ol class="quiz">
<li>
<code>X "foo"</code>
</li>
<li>
<code>Y "foo"</code>
</li>
<li class="correct">
<code>Z (X 1)</code>
</li>
<li>
<code>X (Z 1)</code>
</li>
</ol>
<p>What is the type of this function?</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>f (_<span class="op">:</span><span class="dt">Just</span> x<span class="op">:</span>_) <span class="ot">=</span> x</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a>f _ <span class="ot">=</span> <span class="dt">False</span></span></code></pre></div>
<ol class="quiz">
<li>
<code>Maybe a -> a</code>
</li>
<li>
<code>[Maybe a] -> a</code>
</li>
<li>
<code>[Maybe a] -> Bool</code>
</li>
<li class="correct">
<code>[Maybe Bool] -> Bool</code>
</li>
</ol>
<p>What is the type of this function?</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>f x y <span class="ot">=</span> x<span class="op">-</span>y <span class="op">==</span> <span class="dv">0</span></span></code></pre></div>
<ol class="quiz">
<li class="correct">
<code>(Num a, Eq a) => a -> a -> Bool</code>
</li>
<li>
<code>Num a => a -> a -> Bool</code>
</li>
<li>
<code>Eq a => a -> a -> Bool</code>
</li>
<li>
<code>a -> a -> Bool</code>
</li>
</ol>
<p>Which of the following types can <code>x</code> have in order for <code>x (&&) y</code> to not be a type error?</p>
<ol class="quiz">
<li>
<code>Bool</code>
</li>
<li>
<code>Bool -> Bool -> Bool</code>
</li>
<li class="correct">
<code>(Bool -> Bool -> Bool) -> Bool -> Bool</code>
</li>
</ol>
</section>
<section id="working-on-the-exercises" data-number="1.7">
<h2 data-number="9.7"><span class="header-section-number">9.7</span> Working on the Exercises</h2>
<p>Here’s a short recap of how to work on the exercises. The system is the same as for part 1 of the course.</p>
<ol type="1">
<li>Clone the GitHub repository <a href="https://github.com/moocfi/haskell-mooc" class="uri">https://github.com/moocfi/haskell-mooc</a></li>
<li>Go to the <code>exercises/</code> directory</li>
<li>Run <code>stack build</code> to download dependencies</li>
<li>Edit the file <code>Set9a.hs</code></li>
<li>Check your answers by running <code>stack runhaskell Set9aTest.hs</code></li>
<li>Return your answers on the <a href="https://haskell.mooc.fi/submit.php">Submit page</a></li>
<li>Repeat as necessary. You can work on the exercise sets in any order, and you can return the sets as many times as you want!</li>
<li>You can see total score on the <a href="https://haskell.mooc.fi/status.php">My status page</a></li>
</ol>
</section>
<section id="exercises" data-number="1.8">
<h2 data-number="9.8"><span class="header-section-number">9.8</span> Exercises</h2>
<ul>
<li><a href="https://github.com/moocfi/haskell-mooc/blob/master/exercises/Set9a.hs">Set9a</a> - small exercises that recap part 1 of the course</li>
<li><a href="https://github.com/moocfi/haskell-mooc/blob/master/exercises/Set9b.hs">Set9b</a> - let’s solve the N Queens puzzle!</li>
</ul>
</section>
</section>
<section id="lecture-10-reductionism" data-number="2">
<h1 data-number="10"><span class="header-section-number">10</span> Lecture 10: Reductionism</h1>
<ul>
<li>Purity</li>
<li>Laziness</li>
<li>Haskell evaluation</li>
</ul>
<section id="laziness-purity" data-number="2.1">
<h2 data-number="10.1"><span class="header-section-number">10.1</span> Laziness & Purity</h2>
<p>Purity and laziness were mentioned as key features of Haskell in the beginning of part 1. Let’s take a closer look at them.</p>
<p>Haskell is a <em>pure</em> functional language. This means that the value <code>f x y</code> is always the same for given <code>x</code> and <code>y</code>. In other words, the values of <code>x</code> and <code>y</code> uniquely determine the value of <code>f x y</code>. This property is also called <em>referential transparency</em>.</p>
<p>Purity also means that there are no side effects: you can’t have the evaluation of <code>f x y</code> read a line from the user - the line would be different on different invocations of <code>f</code> and would affect the return value, breaking referential transparency! Obviously you need side effects to actually get something done. We’ll get back to how Haskell handles side effects later.</p>
<p>Haskell is a <em>lazy</em> language. This means that a value is not evaluated if it is not needed. An example illustrates this best. Consider these two functions:</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>f x <span class="ot">=</span> f x <span class="co">-- infinite recursion</span></span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a>g x y <span class="ot">=</span> x</span></code></pre></div>
<p>Evaluating <code>f 1</code> does not halt due to the infinite recursion. However, this works:</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>g <span class="dv">2</span> (f <span class="dv">1</span>) <span class="op">==></span> <span class="dv">2</span></span></code></pre></div>
<p>Laziness is not a problem because Haskell is pure. Only the result of the function matters, not the side effects. So if the result of a function is not used, we can simply not evaluate it without changing the meaning (semantics) of a program. Well okay, sometimes we get a terminating program instead of one that goes on for ever, but adding laziness never makes a functioning Haskell program break.</p>
<p>If you’re interested in the theory behind this, check out the <a href="https://en.wikipedia.org/wiki/Church%E2%80%93Rosser_theorem">Church-Rosser theorem</a> or the Haskell Wiki article <a href="https://wiki.haskell.org/Lazy_vs._non-strict">Lazy vs. non-strict</a>.</p>
</section>
<section id="equational-reasoning" data-number="2.2">
<h2 data-number="10.2"><span class="header-section-number">10.2</span> Equational Reasoning</h2>
<p>Referential transparency, the feature that an expression always returns the same value for the same inputs, is a very powerful property that we can leverage to <em>reason about programs</em>.</p>
<p>In a C-style language, we might write a procedure that may not always return the same value for the same arguments:</p>
<pre><code>int c = 0;
int funny(int x) {
return x + c++;
}</code></pre>
<p>The expression <code>c++</code> increments the value of <code>c</code> and returns the old value of <code>c</code>. The next time it is evaluated, the value of <code>c</code> has increased by one. This means that depending on the current value of <code>c</code>, <code>funny(0)</code> might return <code>0</code>, <code>1</code>, <code>2</code>, or any other integer value. (It might even return negative values if <code>c</code> overflows!)</p>
<p>In some situations this kind of behaviour with side-effects may be useful, but there are also times when it is more important to be able to reason about the code easily. The advantage of pure functions is that they can be analyzed using basic mathematical techniques. Sometimes applying math to our functions can even reveal simplifications or optimisations we otherwise wouldn’t have thought about.</p>
<p>Consider the following expression:</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> (<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">map</span> (<span class="op">-</span><span class="dv">1</span>)</span></code></pre></div>
<p>This expression can be simplified to just <code>reverse</code>. We begin by establishing some helpful facts (or <em>lemmas</em>). First, suppose that we know</p>
<ol type="1">
<li><code>map id === id</code></li>
<li><code>map f . map g === map (f.g)</code></li>
<li><code>reverse . map f === map f . reverse</code></li>
</ol>
<p>The fourth fact that we’re going to need is the following:</p>
<ol start="4" type="1">
<li><code>(+1) . (-1) === id</code></li>
</ol>
<p>We can prove fact 4 by reasoning about how <code>(+1) . (-1)</code> behaves for an arbitrary input <code>x</code>:</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>((<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> (<span class="op">-</span><span class="dv">1</span>)) x <span class="op">===</span> ((<span class="op">+</span><span class="dv">1</span>) ((<span class="op">-</span><span class="dv">1</span>) x))</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a> <span class="op">===</span> ((<span class="op">+</span><span class="dv">1</span>) (x <span class="op">-</span> <span class="dv">1</span>))</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a> <span class="op">===</span> (x <span class="op">-</span> <span class="dv">1</span>) <span class="op">+</span> <span class="dv">1</span></span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a> <span class="op">===</span> x</span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a> <span class="op">===</span> <span class="fu">id</span> x</span></code></pre></div>
<p>Because we didn’t assume anything about <code>x</code>, we may conclude that the above chain of equations holds for <em>every</em> <code>x</code>. Thus,</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>(<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> (<span class="op">-</span><span class="dv">1</span>) <span class="op">===</span> <span class="fu">id</span></span></code></pre></div>
<p>For those who are familiar with the technique of <em>proof by induction</em>, it is a fun exercise to prove the first three facts also. This course doesn’t discuss induction proofs, though, so don’t sweat if you don’t know induction.</p>
<p>Now, from facts 1-4 it follows that</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">map</span> (<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> <span class="fu">reverse</span> <span class="op">.</span> <span class="fu">map</span> (<span class="op">-</span><span class="dv">1</span>)</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> <span class="fu">map</span> (<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> (<span class="fu">reverse</span> <span class="op">.</span> <span class="fu">map</span> (<span class="op">-</span><span class="dv">1</span>)) <span class="co">-- By associativity of (.)</span></span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> <span class="fu">map</span> (<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> (<span class="fu">map</span> (<span class="op">-</span><span class="dv">1</span>) <span class="op">.</span> <span class="fu">reverse</span>) <span class="co">-- By fact 3</span></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> (<span class="fu">map</span> (<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> <span class="fu">map</span> (<span class="op">-</span><span class="dv">1</span>)) <span class="op">.</span> <span class="fu">reverse</span> <span class="co">-- By associativity of (.)</span></span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> <span class="fu">map</span> ((<span class="op">+</span><span class="dv">1</span>) <span class="op">.</span> (<span class="op">-</span><span class="dv">1</span>)) <span class="op">.</span> <span class="fu">reverse</span> <span class="co">-- By fact 2</span></span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> <span class="fu">map</span> <span class="fu">id</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="co">-- By fact 4</span></span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> <span class="fu">id</span> <span class="op">.</span> <span class="fu">reverse</span> <span class="co">-- By fact 1</span></span>
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a><span class="op">===</span> <span class="fu">reverse</span> <span class="co">-- By the definition of id</span></span></code></pre></div>
<p>This course won’t go into details about proving things about programs, but it’s good to know that pure functional programming is very compatible with analysis like this.</p>
</section>
<section id="infinite-lists" data-number="2.3">
<h2 data-number="10.3"><span class="header-section-number">10.3</span> Infinite Lists</h2>
<p>The benefits of laziness are best demonstrated with some examples involving <em>infinite lists</em>. Let’s start with <code>repeat 1</code>, which generates an infinite list of <code>1</code>s. If we try to tell GHCi to print the value <code>repeat 1</code>, it will just keep printing <code>1</code>s for ever until we interrupt it using Control-C:</p>
<div class="sourceCode" id="cb44"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">repeat</span> <span class="dv">1</span></span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span></span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a><span class="op">^</span><span class="dt">C</span></span></code></pre></div>
<p>However, due to laziness, we can work with infinite lists and write computations that end. We just need to use a finite number of elements from the infinite list. Here are some examples:</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">take</span> <span class="dv">10</span> <span class="op">$</span> <span class="fu">repeat</span> <span class="dv">1</span></span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>]</span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">take</span> <span class="dv">20</span> <span class="op">$</span> <span class="fu">repeat</span> <span class="dv">1</span></span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>]</span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">repeat</span> <span class="dv">1</span> <span class="op">!!</span> <span class="dv">13337</span></span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span></span></code></pre></div>
<p>An infinite list that just repeats one element can sometimes be necessary, but it’s kind of pointless. Let’s look at some more useful infinite lists next. You can use the <code>[n..]</code> syntax to generate an infinite list of numbers, starting from <code>n</code>:</p>
<div class="sourceCode" id="cb46"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">take</span> <span class="dv">20</span> [<span class="dv">0</span><span class="op">..</span>]</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a>[<span class="dv">0</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">10</span>,<span class="dv">11</span>,<span class="dv">12</span>,<span class="dv">13</span>,<span class="dv">14</span>,<span class="dv">15</span>,<span class="dv">16</span>,<span class="dv">17</span>,<span class="dv">18</span>,<span class="dv">19</span>]</span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">take</span> <span class="dv">10</span> <span class="op">.</span> <span class="fu">map</span> (<span class="dv">2</span><span class="op">^</span>) <span class="op">$</span> [<span class="dv">0</span><span class="op">..</span>]</span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">8</span>,<span class="dv">16</span>,<span class="dv">32</span>,<span class="dv">64</span>,<span class="dv">128</span>,<span class="dv">256</span>,<span class="dv">512</span>]</span></code></pre></div>
<p>The function <code>cycle</code> repeats elements from the given list over and over again. It can be useful when dealing with rotations or cycles.</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">take</span> <span class="dv">21</span> <span class="op">$</span> <span class="fu">cycle</span> <span class="st">"asdf"</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="st">"asdfasdfasdfasdfasdfa"</span></span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">take</span> <span class="dv">4</span> <span class="op">.</span> <span class="fu">map</span> (<span class="fu">take</span> <span class="dv">4</span>) <span class="op">.</span> tails <span class="op">$</span> <span class="fu">cycle</span> <span class="st">"asdf"</span></span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a>[<span class="st">"asdf"</span>,<span class="st">"sdfa"</span>,<span class="st">"dfas"</span>,<span class="st">"fasd"</span>]</span></code></pre></div>
<section id="example-transaction-numbers" data-number="2.3.1">
<h3 data-number="10.3.1"><span class="header-section-number">10.3.1</span> Example: Transaction Numbers</h3>
<p>As a more concrete example of how <code>cycle</code> is useful, let’s look at computing the check digit of Finnish bank transfer transaction numbers (<a href="https://fi.wikipedia.org/wiki/Tilisiirto#Viitenumero">viitenumero</a>). A transaction number consists of any number of digits, followed by a single check digit. The check digit is checked by multiplying the digits (from right to left) with the numbers 7, 3, 1, 7, 3, 1 and so on, and summing the results. If the result of the sum <em>plus the check digit</em> is divisible by 10, the number is valid.</p>
<p>Here’s a concrete example. <code>116127</code> is a valid transaction number. The computation goes like this:</p>
<pre><code>digits: 1 1 6 1 2
* * * * *
multipliers: 3 7 1 3 7
3+ 7+ 6+ 3+14 = 33
check digit is 7, 33+7=40 is divisible by 10, valid</code></pre>
<p>Here’s the Haskell code for a transaction number checker. Note how we use use the infinite list <code>cycle [7,3,1]</code> for the multipliers.</p>
<div class="sourceCode" id="cb49"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="ot">viitenumeroCheck ::</span> [<span class="dt">Int</span>] <span class="ot">-></span> <span class="dt">Bool</span></span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a>viitenumeroCheck allDigits <span class="ot">=</span> <span class="fu">mod</span> (checksum<span class="op">+</span>checkDigit) <span class="dv">10</span> <span class="op">==</span> <span class="dv">0</span></span>
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span> (checkDigit<span class="op">:</span>digits) <span class="ot">=</span> <span class="fu">reverse</span> allDigits</span>
<span id="cb49-4"><a href="#cb49-4" aria-hidden="true" tabindex="-1"></a> multipliers <span class="ot">=</span> <span class="fu">cycle</span> [<span class="dv">7</span>,<span class="dv">3</span>,<span class="dv">1</span>]</span>
<span id="cb49-5"><a href="#cb49-5" aria-hidden="true" tabindex="-1"></a> checksum <span class="ot">=</span> <span class="fu">sum</span> <span class="op">$</span> <span class="fu">zipWith</span> (<span class="op">*</span>) multipliers digits</span></code></pre></div>
<div class="sourceCode" id="cb50"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>viitenumeroCheck [<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">6</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">7</span>] <span class="op">==></span> <span class="dt">True</span></span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a>viitenumeroCheck [<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">6</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">8</span>] <span class="op">==></span> <span class="dt">False</span></span></code></pre></div>
</section>
<section id="example-finding-a-power" data-number="2.3.2">
<h3 data-number="10.3.2"><span class="header-section-number">10.3.2</span> Example: Finding a Power</h3>
<p>Finally, here’s how you would find the first power of 3 that’s larger than 100.</p>
<div class="sourceCode" id="cb51"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Prelude</span><span class="op">></span> <span class="fu">head</span> <span class="op">.</span> <span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) <span class="op">$</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">0</span><span class="op">..</span>]</span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="dv">243</span></span></code></pre></div>
<p>Let’s go through how this works step by step. Note how map and filter are processing the list lazily, one element at a time, as needed. This is similar to how <em>generators</em> or <em>iterators</em> work in languages like Python or Java.</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">0</span><span class="op">..</span>]))</span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) (<span class="dv">0</span><span class="op">:</span>[<span class="dv">1</span><span class="op">..</span>]))) <span class="co">-- evaluate first element of the lazy list</span></span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="dv">1</span> <span class="op">:</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">1</span><span class="op">..</span>])) <span class="co">-- map processes the element</span></span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">1</span><span class="op">..</span>])) <span class="co">-- filter drops the element</span></span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) (<span class="dv">1</span><span class="op">:</span>[<span class="dv">2</span><span class="op">..</span>]))) <span class="co">-- evaluate second element of the lazy list</span></span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="dv">3</span> <span class="op">:</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">2</span><span class="op">..</span>])) <span class="co">-- map processes the element</span></span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">2</span><span class="op">..</span>])) <span class="co">-- filter drops the element</span></span>
<span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a><span class="co">-- let's take bigger steps now</span></span>
<span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="dv">9</span> <span class="op">:</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">3</span><span class="op">..</span>])) <span class="co">-- map processes, filter will drop</span></span>
<span id="cb52-10"><a href="#cb52-10" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="dv">27</span> <span class="op">:</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">4</span><span class="op">..</span>])) <span class="co">-- map processes, filter will drop</span></span>
<span id="cb52-11"><a href="#cb52-11" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="dv">81</span> <span class="op">:</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">5</span><span class="op">..</span>])) <span class="co">-- map processes, filter will drop</span></span>
<span id="cb52-12"><a href="#cb52-12" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="dv">243</span> <span class="op">:</span> <span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">6</span><span class="op">..</span>])) <span class="co">-- map processes</span></span>
<span id="cb52-13"><a href="#cb52-13" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="fu">head</span> (<span class="dv">243</span> <span class="op">:</span> <span class="fu">filter</span> (<span class="op">></span><span class="dv">100</span>) (<span class="fu">map</span> (<span class="dv">3</span><span class="op">^</span>) [<span class="dv">6</span><span class="op">..</span>])) <span class="co">-- filter lets the value through</span></span>
<span id="cb52-14"><a href="#cb52-14" aria-hidden="true" tabindex="-1"></a><span class="op">==></span> <span class="dv">243</span> <span class="co">-- head returns the result</span></span></code></pre></div>
</section>
</section>
<section id="how-does-haskell-work" data-number="2.4">
<h2 data-number="10.4"><span class="header-section-number">10.4</span> How does Haskell Work?</h2>
<p>Laziness will probably feel a bit magical to you right now. You might wonder how it can be implemented. Haskell evaluation is remarkably simple, it’s just different than what you might be used to. Let’s dig in.</p>
<p>In most other programming languages (like Java, C or Python), evaluation proceeds inside-out. Arguments to functions are evaluated before the function.</p>
<p>Haskell evaluation proceeds outside-in instead of inside-out. The definition of the outermost function in an expression is applied without evaluating any arguments. Here’s a concrete example with toy functions <code>f</code> and <code>g</code>:</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="ot">g ::</span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span></span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a>g x y <span class="ot">=</span> y<span class="op">+</span><span class="dv">1</span></span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a><span class="ot">f ::</span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span> <span class="ot">-></span> <span class="dt">Int</span></span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a>f a b c <span class="ot">=</span> g (a<span class="op">*</span><span class="dv">1000</span>) c</span></code></pre></div>
<p>Inside-out (normal) evaluation:</p>
<div class="sourceCode" id="cb54"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>f <span class="dv">1</span> (<span class="dv">1234</span><span class="op">*</span><span class="dv">1234</span>) <span class="dv">2</span></span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a> <span class="co">-- evaluate arguments to f</span></span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> f <span class="dv">1</span> <span class="dv">1522756</span> <span class="dv">2</span></span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a> <span class="co">-- evaluate f</span></span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> g (<span class="dv">1</span><span class="op">*</span><span class="dv">1000</span>) <span class="dv">2</span></span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a> <span class="co">-- evaluate arguments to g</span></span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> g <span class="dv">1000</span> <span class="dv">2</span></span>
<span id="cb54-8"><a href="#cb54-8" aria-hidden="true" tabindex="-1"></a> <span class="co">-- evaluate g</span></span>
<span id="cb54-9"><a href="#cb54-9" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> <span class="dv">2</span><span class="op">+</span><span class="dv">1</span></span>
<span id="cb54-10"><a href="#cb54-10" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> <span class="dv">3</span></span></code></pre></div>
<p>Haskell outside-in evaluation:</p>
<div class="sourceCode" id="cb55"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>f <span class="dv">1</span> (<span class="dv">1234</span><span class="op">*</span><span class="dv">1234</span>) <span class="dv">2</span></span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a> <span class="co">-- evaluate f without evaluating arguments</span></span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> g (<span class="dv">1</span><span class="op">*</span><span class="dv">1000</span>) <span class="dv">2</span></span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> <span class="co">-- evaluate g without evaluating arguments</span></span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> <span class="dv">2</span><span class="op">+</span><span class="dv">1</span></span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> <span class="dv">3</span></span></code></pre></div>
<p>Note how the unused calculations <code>1234*1234</code> and <code>1*1000</code> didn’t get evaluated. This is why laziness is often helpful.</p>
<section id="pattern-matching-drives-evaluation" data-number="2.4.1">
<h3 data-number="10.4.1"><span class="header-section-number">10.4.1</span> Pattern Matching Drives Evaluation</h3>
<p>Let’s look at a more involved example, with pattern matching and more complex data (lists). Pattern matching drives Haskell evaluation in a very concrete way, as we’ll see. Here are some functions that we’ll use. They’re familiar from the Prelude, but I’ll give them simple definitions.</p>
<div class="sourceCode" id="cb56"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="fu">not</span> <span class="dt">True</span> <span class="ot">=</span> <span class="dt">False</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="fu">not</span> <span class="dt">False</span> <span class="ot">=</span> <span class="dt">True</span></span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> f [] <span class="ot">=</span> []</span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> f (x<span class="op">:</span>xs) <span class="ot">=</span> f x <span class="op">:</span> <span class="fu">map</span> f xs</span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span> [] <span class="ot">=</span> <span class="dv">0</span></span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span> (x<span class="op">:</span>xs) <span class="ot">=</span> <span class="dv">1</span><span class="op">+</span><span class="fu">length</span> xs</span></code></pre></div>
<p>Here’s the inside-out evaluation of an expression:</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span> (<span class="fu">map</span> <span class="fu">not</span> (<span class="dt">True</span><span class="op">:</span><span class="dt">False</span><span class="op">:</span>[]))</span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a> <span class="op">==></span> <span class="fu">length</span> (<span class="fu">not</span> <span class="dt">True</span> <span class="op">:</span> <span class="fu">not</span> <span class="dt">False</span> <span class="op">:</span> []) <span class="co">-- evaluate call to map</span></span>