-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinciples-of-programming-languages-reference.tex
More file actions
executable file
·1686 lines (1337 loc) · 46.6 KB
/
principles-of-programming-languages-reference.tex
File metadata and controls
executable file
·1686 lines (1337 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
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
\documentclass[a4paper,landscape,10pt]{article}
\usepackage{settings}
% TODO: when everything has been written, space correctly the content by breaking the columns via the (custom)\breakcolumn command
\begin{document}
\section{Racket}
\begin{multicols*}{2}
\subsection{Comments}
\begin{itemize}
\item single line comment: \iracket{;}
\item multi-line comment: \iracket{\#| ... |\#}
\item multi-line comments can be nested
\end{itemize}
\begin{racket}
; single line comment
#|
multi-line-comment
can span
multiple lines
end of comment
|#
\end{racket}
\subsection{Data types}
\begin{itemize}
\item typing is dynamic
\item types:
\begin{itemize}
\item boolean: \iracket{#\\t, #\\f}
\item integer: \iracket{9125}
\item binary: \iracket{#b10001110100101}
\item octal: \iracket{#o21645}
\item hexadecimal: \iracket{#x23a5}
\item real: \iracket{91.25}
\item rational: \iracket{91/25}
\item complex: \iracket{91+25i}
\item character: \lstinline[language=CustomRacket, mathescape=true]!#\A, #\$\lambda$, #\u30BB!
\item null element: \iracket{'(), null}
\item string: \iracket{"Hello, world!"}
\end{itemize}
\end{itemize}
\begin{racket}
(define x 5) ; => x = 5
(define y "Hello, world!") ; => y = "Hello, world!"
(define z #\t) ; => z = #\t
(define w #\A) ; => w = #\A
null ; => '()
\end{racket}
\breakcolumn
\subsection{Variables}
\begin{itemize}
\item variables are immutable
\item parallel binding: \iracket{let}
\item serial binding: \iracket{let*}
\item recursive binding: \iracket{letrec}
\end{itemize}
\begin{racket}
(let ((x 5) (y 2)) (printf "~a ~a~n" x y)) ; => 5 2
(let* ((x 1) (y (add1 x))) (printf "~a ~a~n" x y)) ; => 1 2
\end{racket}
\subsubsection{Datum evaluation}
\begin{itemize}
\item \iracket{quote <datum>} or \iracket{'<datum>} leaves the datum as-is
\item \iracket{unquote <datum>} or \iracket{,<datum>} is the opposite of \iracket{quote}
\item \iracket{quasiquote <datum>} or \iracket{,@<datum>} allows to apply the unquote where needed
\end{itemize}
\begin{racket}
'(1 2 3); => (1 2 3)
(1 ,(+ 1 1) 3) ; => '(1 2 3)
\end{racket}
\subsubsection{Equivalence}
\begin{itemize}
\item numbers equivalence: \iracket{=}
\item objects or numbers equivalence: \iracket{eq?}
\item objects equivalence: \iracket{eqv?}
\item objects equivalence: \iracket{equal?}
\end{itemize}
\begin{racket}
(= 1 1) ; => #\t
(eq? 1 0) ; => #\f
(eqv? 'yes 'yes) ; => #\t
(equal? 'yes 'no) ; => #\f
\end{racket}
\subsubsection{Basic operations}
\begin{itemize}
\item all operations are in prefix notation \iracket{<operator> <operand> ...}
\end{itemize}
\breakcolumn
\paragraph{Operations on numbers}
\begin{itemize}
\item arithmetic operations: \iracket{+}, \iracket{-}, \iracket{*}, \iracket{/}
\item exponentiation: \iracket{expt}
\item exponentiation by \(e\): \iracket{exp}
\item logarithm: \iracket{log}
\item quotient: \iracket{quotient}
\item remainder: \iracket{remainder}
\item largest and smallest of two numbers: \iracket{max}, \iracket{min}
\item add \(1\): \iracket{add1}
\item subtract \(1\): \iracket{sub1}
\item greatest common divisor: \iracket{gcd}
\item least common multiple: \iracket{lcm}
\end{itemize}
\begin{racket}
(+ 1 2 3) ; => 6
(- 1 2 3) ; => -4
(expt 2 3) ; => 8
(exp 2) ; => e ** 2 = 7.38905609893065
(log 10) ; => 2.302585092994046
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(max 1 2) ; => 2
(min 1 2) ; => 1
(add1 5) ; => 6
(sub1 5) ; => 4
(gcd 12 18) ; => 6
(lcm 12 18) ; => 36
\end{racket}
\paragraph{Operations on strings}
\begin{itemize}
\item string length: \iracket{string-length}
\item string append: \iracket{string-append}
\item string to list: \iracket{string->list}
\item list to string: \iracket{list->string}
\item get n-th character: \iracket{string-ref}
\end{itemize}
\begin{racket}
(string-length "Hello, world!") ; => 13
(string-append "Hello, " "world!") ; => "Hello, world!"
(string->list "Hello") ; => '(#\H #\e #\l #\l #\o)
(list->string '(#\H #\e #\l #\l #\o)) ; => "Hello"
(string-ref "Hello" 0) ; => #\H
\end{racket}
\paragraph{Operations on bools}
\begin{itemize}
\item logic operations: \iracket{and}, \iracket{or}, \iracket{not}, \iracket{xor}
\item implication: \iracket{implies}
\end{itemize}
\begin{racket}
(and #\t #\f) ; => #\f
(or #\t #\f) ; => #\t
(not #\t) ; => #\f
(xor #\t #\f) ; => #\t
(implies #\t #\f) ; => #\f
\end{racket}
\subsubsection{Types conversion}
\begin{itemize}
\item inexact and exact: \iracket{inexact->exact}, \iracket{exact->inexact}
\item integer and float: \iracket{integer->float}, \iracket{float->integer}
\item integer and rational: \iracket{integer->rational}, \iracket{rational->integer}
\item list and vector: \iracket{list->vector}, \iracket{vector->list}
\item vector and string: \iracket{vector->string}, \iracket{string->vector}
\end{itemize}
\subsection{Predicates}
\begin{itemize}
\item all predicates end with \iracket{?}
\item checks if a number is even: \iracket{even?}
\item checks if a number is odd: \iracket{odd?}
\item check if a datum is true: \iracket{true?}
\item check if a datum is false: \iracket{false?}
\item check if a number is positive: \iracket{positive?}
\item check if a number is negative: \iracket{negative?}
\item check if a number is zero: \iracket{zero?}
\item check if an object is immutable \iracket{immutable?}
\end{itemize}
\begin{racket}
(even? 2) ; => #\t
(odd? 2) ; => #\f
(true? #\t) ; => #\t
(false? #\t) ; => #\f
(positive? 1) ; => #\t
(negative? 1) ; => #\f
(zero? 1) ; => #\f
\end{racket}
\breakcolumn
\subsection{Functions}
\begin{itemize}
\item anonymous functions: \iracket{lambda (<arg1> <arg2> ...) <body>}
\item named functions: \iracket{define (<name> <arg1> <arg2> ...) <body>}
\item old way: \iracket{define <name> (lambda (<arg1> <arg2> ...) <body>)}
\end{itemize}
\begin{racket}
; anonymous function
((lambda (x) (+ x 3)) 5); => 8
; named function
(define (add3 x) (+ x 3))
(add3 5) ; => 8
\end{racket}
\subsubsection{Higher order functions}
\begin{itemize}
\item apply a function to each element of a list: \iracket{map <function> <list>}
\item apply a filter: \iracket{filter <predicate> <list>}
\item apply a function to each element of a list and flatten the result: \iracket{apply <function> <list>}
\item fold a list: \iracket{foldl <function> <accumulator> <list>}
\item fold a list: \iracket{foldr <function> <accumulator> <list>}
\item \iracket{foldl} has space complexity \(O(1)\)
\item \iracket{foldr} has space complexity \(O(n)\)
\end{itemize}
\begin{racket}
(map add1 '(1 2 3)) ; => '(2 3 4)
(filter even? '(1 2 3 4)) ; => '(2 4)
(apply append '((1 2) (3 4))) ; => '(1 2 3 4)
(foldl + 0 '(1 2 3)) ; => 6
(foldr + 0 '(1 2 3)) ; => 6
\end{racket}
\subsection{Mutation}
\begin{itemize}
\item all mutators end with \iracket{\!}
\item \iracket{set\!} is used to mutate variables
\item \iracket{vector-set\!} is used to mutate vectors
\end{itemize}
\begin{racket}
(define x 5) ; => x = 5
(set! x 6) ; => x = 6
(define v (vector 2 2 3 4)) ; => v = '#(2 2 3 4)
(vector-set! v 0 1) ; => v = '#(1 2 3 4)
\end{racket}
\breakcolumn
\subsection{Collections}
\subsubsection{Structs}
\begin{itemize}
\item definition: \iracket{struct <struct-name> (<field> ...)}
\item constructor: \iracket{define <name> <struct-name> <field-value> ...}
\item getter: \iracket{<struct-name>-<field-name>}
\item setter: \iracket{set-<struct-name>-<field-name>\!}
\item predicate: \iracket{<struct-name>?}
\item structs and fields are immutable by default
\item use \iracket{#:mutable} keyword on struct or field to make it mutable
\end{itemize}
\begin{racket}
(struct point (x y)) ; => point
(define p (point 1 2)) ; => p = (point 1 2)
(point-x p) ; => 1
(point? p) ; => #\t
(struct mut-point (x y #:mutable)) ; => point
(define mp (mut-point 1 2)) ; => mp = (mut-point 1 2)
(set-mut-point-x! mp 5) ; => mp = (mut-point 5 2)
\end{racket}
\subsubsection{Pairs}
\begin{itemize}
\item definition: \iracket{cons <first> <second>}
\item getter of first element: \iracket{car}
\item getter of second element: \iracket{cdr}
\item car and cdr can be composed: \iracket{cadddr}, \iracket{caaar}, \ldots
\item check if a variable is a pair: \iracket{pair?}
\item pairs are immutable
\end{itemize}
\begin{racket}
(cons 1 2) ; => '(1 . 2)
(car '(1 . 2)) ; => 1
(cdr '(1 . 2)) ; => 2
(pair? '(1 . 2)) ; => #\t
(pair? 1) ; => #\f
(caar '((1 . 2) . 3)) ; => 1
(cadr '((1 . 2) . 3)) ; => 2
(cdar '((1 . 2) . 3)) ; => 2
(cddr '((1 . 2) . 3)) ; => 3
\end{racket}
\breakcolumn
\subsubsection{Lists}
\begin{itemize}
\item lists are composed of pairs
\item manually defined via \iracket{quote}: \iracket{'(1 2 3)}
\item empty list: \iracket{'()}
\item list of length \iracket{n}: \iracket{build-list <n> <procedure>}
\item list of length \iracket{n} with initial value \iracket{<init>}: \iracket{make-list <n> <init>}
\item lists are made by pairs
\begin{itemize} \item the \iracket{car} contains the first value
\item the \iracket{cdr} contains the the rest of the list
\item the last pair has \iracket{cdr} equal to \iracket{'()}
\end{itemize}
\end{itemize}
\begin{racket}
'(1 2 3) ; => '(1 2 3)
'(1 . (2 . (3 . ()))) ; => '(1 2 3)
(build-list 3 (lambda (x) (* x 2))) ; => '(0 2 4)
(make-list 3 7) ; => '(7 7 7)
\end{racket}
\paragraph{Operations on lists}
\begin{itemize}
\item list length: \iracket{length}
\item check if a variable is a list: \iracket{list?}
\item check if a list is empty: \iracket{empty?}
\item add an element at the beginning: \iracket{cons}
\item add an element at the end: \iracket{append}
\item get the elements after the first: \iracket{rest <list>}
\item get the first element: \iracket{first}
\item get the last element: \iracket{last}
\item get the n-th element: \iracket{list-ref <list> <n>}
\item get the elements after the n-th: \iracket{list-tail <list> <pos>}
\item get the first n elements: \iracket{take <list> <n>}
\item get the last n elements: \iracket{drop <list> <n>}
\item count the occurrences of an element: \iracket{count <predicate> <list>}
\item apply a filter: \iracket{filter <predicate> <list>}
\item apply a function to each element: \iracket{map <function> <list>}
\item get the reverse of a list: \iracket{reverse <list>}
\end{itemize}
\breakcolumn
\begin{racket}
(length '(1 2 3)) ; => 3
(list? '(1 2 3)) ; => #\t
(empty? '(1 2 3)) ; => #\f
(cons 1 '(2 3)) ; => '(1 2 3)
(append '(1 2) '(3 4)) ; => '(1 2 3 4)
(first '(1 2 3)) ; => 1
(last '(1 2 3)) ; => 3
(list-ref '(1 2 3) 1) ; => 2
(list-tail '(1 2 3) 1) ; => '(2 3)
(take '(1 2 3) 2) ; => '(1 2)
(drop '(1 2 3) 1) ; => '(2 3)
(count even? '(1 2 3 4)) ; => 2
(filter even? '(1 2 3 4)) ; => '(2 4)
(map add1 '(1 2 3)) ; => '(2 3 4)
(reverse '(1 2 3)) ; => '(3 2 1)
(rest '(1 2 3)) ; => '(2 3)
\end{racket}
\paragraph{Lists folding}
\begin{itemize}
\item lists can be folded from the left with \iracket{foldl}
\item lists can be folded from the right with \iracket{foldr}
\end{itemize}
\begin{racket}
(foldl + 0 '(1 2 3 4)) ; => 10
(foldr * 1 '(1 2 3 4)) ; => 24
\end{racket}
\subsubsection{Vectors}
\begin{itemize}
\item definition: \iracket{#(<element> ...)}
\item getter: \iracket{vector-ref}
\item vector are immutable, fixed size and zero-indexed
\end{itemize}
\begin{racket}
#(1 2 3) ; => '#(1 2 3)
(vector-ref '#(1 2 3) 0) ; => 1
\end{racket}
\breakcolumn
\subsubsection{Sets}
\begin{itemize}
\item definition: \iracket{set <element> ...}
\item convert a list to a set: \iracket{list->set}
\item add an element: \iracket{set-add}
\item remove an element: \iracket{set-remove}
\item test if an element is in the set: \iracket{set-member?}
\item sets don't allow duplicates, are unordered and mutable
\item methods return a new set instead of changing the original one
\end{itemize}
\begin{racket}
(set 1 2 3) ; => '#(1 2 3)
(list->set '(1 2 3)) ; => '#(1 2 3)
(set-add (set 1 2 3) 4) ; => '#(1 2 3 4)
(set-remove (set 1 2 3) 2) ; => '#(1 3)
(set-member? (set 1 2 3) 2) ; => #\t
\end{racket}
\subsubsection{Hash}
\begin{itemize}
\item definition: \iracket{hash <key> <value> ...}
\item add a key-value pair: \iracket{hash-set}
\item remove a key-value pair: \iracket{hash-remove}
\item get a value from a key: \iracket{hash-ref}
\item test if a key is in the hash: \iracket{hash-has-key?}
\end{itemize}
\begin{racket}
(define (h) (hash 'a 1 'b 2))
(hash-set (h) 'c 3) ; => '#hash((a . 1) (b . 2) (c . 3))
(hash-remove (h) 'b) ; => '#hash((a . 1))
(hash-ref (h) 'a) ; => 1
(hash-has-key? (h) 'a) ; => #\t
\end{racket}
\breakcolumn
\subsection{Control flow}
\subsubsection{Conditionals}
\paragraph{if}
\begin{itemize}
\item if: \iracket{if <predicate> <then> <else>}
\item when: \iracket{when <predicate> <then>}
\item unless: \iracket{unless <predicate> <else>}
\end{itemize}
\begin{racket}
(if #\t 1 2) ; => 1
(when #\t 1) ; => 1
(when #\f 1) ; => #<void>
(unless #\t 1) ; => #<void>
(unless #\f 1) ; => 1
\end{racket}
\paragraph{cond - case}
\begin{itemize}
\item cond: \iracket{cond [<predicate> <then>] ... [<else> <else-then>]}
\item case: \iracket{case <value> [<case-clause> <then>] ... [<else> <else-then>]}
\item the \iracket{else} clause is optional
\item in cond, the value is evaluated against each predicate
\item in case, the value is evaluated against each clause whose quote is \iracket{eqv?}
\end{itemize}
\begin{racket}
(case (+ 7 5)
[(1 2 3) 'small]
[(10 11 12) 'big]
[else 'neither]) ; => 'big
(let ((x 0))
(cond ((positive? x) 'positive)
((negative? x) 'negative)
(else 'zero))) ; => 'zero
\end{racket}
\paragraph{pattern matching}
\begin{itemize}
\item match: \iracket{match <value> [<pattern> <then>] ... [_ <else-then>]}
\end{itemize}
\begin{racket}
(define (fizzbuzz? n)
(match (list (remainder n 3) (remainder n 5))
[(list 0 0) 'fizzbuzz]
[(list 0 _) 'fizz]
[(list _ 0) 'buzz]
[_ #\f]))
(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #\f
\end{racket}
\breakcolumn
\subsubsection{Loops}
\paragraph{when}
\begin{itemize}
\item when: \iracket{when <predicate> <then>}
\item also available as named let
\end{itemize}
\begin{racket}
(when #\t (printf "Hello, world!\n")) ; => Hello, world!
(let label ((x 0)) ; initialize x as 0
(when (< x 10) ; iterate while x < 10
(printf "x=~a\n" x) ; print x
(label (+ x 1)))) ; increment x, go back to label
\end{racket}
\paragraph{for}
\begin{itemize}
\item for in a range: \iracket{for ([<var> <start> <end>]) <body>}
\item for over lists: \iracket{for ([<var> <list>]) <body>}
\item for is available for other collections
\end{itemize}
\begin{racket}
(for ([i 10])
(printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
(printf "i=~a\n" i)) ; => i=5, i=6, ...
(for ([i (in-list '(l i s t))])
(displayln i))
(for ([i (in-vector #(v e c t o r))])
(displayln i))
(for ([i (in-string "string")])
(displayln i))
(for ([i (in-set (set 'x 'y 'z))])
(displayln i))
(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3))])
(printf "key:~a value:~a\n" k v))
\end{racket}
\breakcolumn
\subsection{Macros and syntax rules}
\begin{itemize}
\item definition: \iracket{define-syntax((<literals>) [(<syntax-rule> ...), ...])}
\item syntax rules are defined via \iracket{syntax-rules(<pattern> <expansion>)}
\item macros are expanded at compile time
\item the \iracket{...} operator indicates repetitions of patterns
\item the \iracket{_} operator is used to match any syntax object
\end{itemize}
\begin{racket}
(define-syntax while
(syntax-rules () ; no reserved keywords
((_ condition body ...) ; pattern P
(let loop () ; expansion of P
(when condition
((begin body ...
(loop))))))))
\end{racket}
\subsection{Continuations}
\begin{itemize}
\item two ways to call a continuation:
\begin{itemize}
\item \iracket{call-with-current-continuation <procedure>}
\item \iracket{call/cc <procedure>}
\end{itemize}
\item saving the continuation: \iracket{save\! <continuation>}
\end{itemize}
\subsection{Exceptions}
\begin{itemize}
\item exceptions are implemented via continuations
\item raise an exception: \iracket{raise}
\item catch an exception: \iracket{with-handlers}
\end{itemize}
\begin{racket}
(with-handlers
([exn:fail?
(lambda (e) (printf "error: ~a\n" e))])
(raise (exn:fail "error message")))
; => error: error message
\end{racket}
\breakcolumn
\subsection{Examples}
\subsubsection{For via named let}
\begin{racket}
(define (loop i j)
(when (< i j)
(printf "i=~a\n" i)
(loop (add1 i) j)))
(loop 5 10) ; => i=5, i=6, i=7, i=8, i=9
\end{racket}
\subsubsection{Call with current continuation}
\begin{itemize}
\item This example shows how to use continuations to implement a break statement via garbage collection strategy
\item Other strategies are also possible, such as using a stack
\end{itemize}
\begin{racket}
; for with break definition
(define-syntax For
(syntax-rules (from to break : do)
((_ var from min to max break : br-sym do body ...)
(let * ((min1 min)
(max1 max)
(inc (if (< min1 max1) + -)))
(call/cc (lambda (br-sym)
(let loop ((var min1))
body ...
(unless (= var max1)
(loop (inc var 1))))))))))
; code usage
(For i from 1 to 10 break : get-out
do (displayln i)
(when (= i 5)
(get-out)))
\end{racket}
\end{multicols*}
\clearpage
\section{Haskell}
\begin{multicols*}{2}
\subsection{About Nomenclature}
To avoid further confusion, here is a comparison between Haskell and generic OOP \textit{(object oriented programming)} language nomenclature:
\begin{itemize}
\item an OOP \texttt{class} is a Haskell \texttt{interface}
\item an OOP \texttt{type} is a Haskell \texttt{class}
\item an OOP \texttt{value} is a Haskell \texttt{object}
\item an OOP \texttt{method} is a Haskell \texttt{method}
\end{itemize}
\subsection{Comments}
\begin{itemize}
\item single line comment: \ihaskell{--}
\item multi-line comment: \ihaskell{\{- ... -\}}
\end{itemize}
\begin{haskell}
-- single line comment
{-
multi-line comment
can span
multiple lines
-}
\end{haskell}
\subsection{Data types}
\begin{itemize}
\item data type is inferred automatically by the compiler
\item data type can be specified explicitly via type annotations \ihaskell{::}
\item types:
\begin{itemize}
\item boolean: \ihaskell{True}, \ihaskell{False}
\item integer: \ihaskell{1, 2, 3}
\item float, double: \ihaskell{1.0, 2.0, 3.0}
\item complex: \ihaskell{1 :+ 2, 2 :+ 3, 3 :+ 4}
\item character: \ihaskell{'a', 'b', 'c'}
\item string: \ihaskell{["a", "b", "c"]} or \ihaskell{"abc"}
\item lists: \ihaskell{[1, 2, 3]}
\item tuples: \ihaskell{(1, 2, 3)}
\end{itemize}
\end{itemize}
\subsubsection{User defined types}
\begin{itemize}
\item sum types: \ihaskell{data <type> = <constructor1> | <constructor2> | ...}
\item product type: \ihaskell{data <type> = <constructor> <field1> <field2> ...}
\end{itemize}
\begin{haskell}
data Bool = True | False -- sum type
data Point = Point Float Float -- product type
\end{haskell}
\subsubsection{Recursive types}
\begin{itemize}
\item syntax: \ihaskell{data <type> = <constructor> <field1> <field2> ... <type>}
\end{itemize}
\begin{haskell}
data Tree a = Empty | Node a (Tree a) (Tree a)
\end{haskell}
\subsubsection{Type Synonyms}
\begin{itemize}
\item syntax: \ihaskell{type <name> = <type>}
\end{itemize}
\begin{haskell}
type Point = [(Float, Float)]
\end{haskell}
\subsection{Variables}
\begin{itemize}
\item variables are immutable
\item recursive binding: \ihaskell{let}
\item declaration with function body: \ihaskell{where}
\end{itemize}
\begin{haskell}
let x = 5 in x + 1 -- => 6
let x = 5 y = 2
in x + y -- => 7
f x = x + 1
where x = 5 -- => 6
\end{haskell}
\subsubsection{Equivalence}
\begin{itemize}
\item equivalence between objects, numbers, strings and characters: \ihaskell{==}
\end{itemize}
\subsubsection{Basic operations}
\begin{itemize}
\item prefix operators can be converted into infix notation via backticks \ihaskell{`<operator>`}
\item infix operators can be converted into prefix notation via parentheses \ihaskell{(<operator>)}
\item symbol \ihaskell{\$} is used to avoid parentheses by applying the function to the right first
\item symbol \ihaskell{.} is used to compose functions
\end{itemize}
\breakcolumn
\paragraph{Operations on numbers}
\begin{itemize}
\item arithmetic operations: \ihaskell{+}, \ihaskell{-}, \ihaskell{*}, \ihaskell{/}
\item exponentiation: \ihaskell{**}
\item exponentiation by e: \ihaskell{exp}
\item logarithm: \ihaskell{log}
\item quotient: \ihaskell{quot}
\item remainder: \ihaskell{rem}
\item largest and smallest of two numbers: \ihaskell{max}, \ihaskell{min}
\item add \(1\): \ihaskell{succ}
\item subtract \(1\): \ihaskell{pred}
\item greatest common divisor: \ihaskell{gcd}
\item least common multiple: \ihaskell{lcm}
\end{itemize}
\begin{haskell}
3 + 2 -- => 5
3 - 2 -- => 1
3 * 2 -- => 6
3 / 2 -- => 1.5
3 ** 2 -- => 9.0
exp 2 -- => 7.38905609893065
log 10 -- => 2.302585092994046
quot 5 2 -- => 2
rem 5 2 -- => 1
max 1 2 -- => 2
min 1 2 -- => 1
succ 5 -- => 6
pred 5 -- => 4
gcd 12 18 -- => 6
lcm 12 18 -- => 36
\end{haskell}
\paragraph{Operations on strings}
\begin{itemize}
\item string length: \ihaskell{length}
\item string append: \ihaskell{++}
\item string to list: \ihaskell{words}
\item list to string: \ihaskell{unwords}
\end{itemize}
\begin{haskell}
length "Hello, world!" -- => 13
"Hello, " ++ "world!" -- => "Hello, world!"
words "Hello world!" -- => ["Hello", "world!"]
unwords ["Hello", "world!"] -- => "Hello world!"
\end{haskell}
\breakcolumn
\paragraph{Operations on bools}
\begin{itemize}
\item logic operations: \ihaskell{&&}, \ihaskell{||}, \ihaskell{not}, \ihaskell{xor}
\item implication: \ihaskell{implies}
\end{itemize}
\begin{haskell}
True && False -- => False
True || False -- => True
not True -- => False
xor True False -- => True
implies True False -- => False
\end{haskell}
\subsection{Functions}
\begin{itemize}
\item lambda functions: \ihaskell{\\<name> <arg1> <arg2> ... -> <body>}
\item functions are defined as sequences of equations
\begin{itemize}
\item arguments are matched with the right parts of equations, top to bottom
\item if the match succeeds, the function body is called
\end{itemize}
\end{itemize}
\begin{haskell}
\x y -> x + y -- => \x y -> x + y
length :: [a] -> Integer -- type annotation
length [] = 0
length (x:xs) = 1 + length xs
1 == 1 -- => True
"abc" == "abc" -- => True
\end{haskell}
\subsection{Collections}
\subsubsection{Fields}
\begin{itemize}
\item fields can be accessed either by label or by position
\end{itemize}
\subsubsection{Lists}
\begin{itemize}
\item lists are composed of pairs
\item manual definition: \ihaskell{[1, 2, 3]}
\item empty list: \ihaskell{[]}
\end{itemize}
\paragraph{Operations on lists}
\begin{itemize}
\item list length: \ihaskell{length <list>}
\item get the reverse of a list: \ihaskell{reverse <list>}
\item concatenate two lists: \ihaskell{<list1> ++ <list2>}
\item add an element: \ihaskell{<element> : <list>}
\item get the first element: \ihaskell{head <list>}
\item get the last element: \ihaskell{last <list>}
\item get the n-th element: \ihaskell{<list> \!\! <n>}
\item get the first n elements: \ihaskell{take <list> <n>}
\item delete the first \ihaskell{n} elements: \ihaskell{drop <n> <list>}
\item get all the elements after the first: \ihaskell{tail}
\item split a list in two: \ihaskell{splitAt <position> <list>}
\item apply a filter: \ihaskell{filter <predicate> <list>}
\item apply a function to each element: \ihaskell{map <function> <list>}
\item sum a list: \ihaskell{sum <list>}
\item product of a list: \ihaskell{product <list>}
\item check if a list is empty: \ihaskell{null <list>}
\item check if an element is in a list: \ihaskell{elem <element> <list>}
\item check if all elements of a list satisfy a predicate: \ihaskell{all <predicate> <list>}
\item check if at least one element of a list satisfies a predicate: \ihaskell{any <predicate> <list>}
\item zip two lists: \ihaskell{zip <list1> <list2>}
\end{itemize}
\begin{haskell}
length [1, 2, 3] -- => 3
reverse [1, 2, 3] -- => [3, 2, 1]
[1, 2, 3] ++ [4, 5, 6] -- => [1, 2, 3, 4, 5, 6]
1 : [2, 3] -- => [1, 2, 3]
head [1, 2, 3] -- => 1
last [1, 2, 3] -- => 3
[1, 2, 3] !! 1 -- => 2
take 2 [1, 2, 3] -- => [1, 2]
drop 2 [1, 2, 3] -- => [3]
tail [1, 2, 3] -- => [2, 3]
splitAt 1 [1, 2, 3] -- => ([1], [2, 3])
filter even [1, 2, 3, 4] -- => [2, 4]
map (+1) [1, 2, 3] -- => [2, 3, 4]
sum [1, 2, 3] -- => 6
product [1, 2, 3] -- => 6
null [] -- => True
elem 1 [1, 2, 3] -- => True
all even [2, 4, 6] -- => True
any even [1, 2, 3] -- => True
zip [1, 2, 3] [4, 5, 6] -- => [(1, 4), (2, 5), (3, 6)]
\end{haskell}
\paragraph{Range notation}
\begin{itemize}
\item finite list: \ihaskell{[<start>..<end>]}
\item finite list with step: \ihaskell{[<start>,<step>..<end>]}
\item infinite list: \ihaskell{[<start>..]}
\item infinite list with step: \ihaskell{[<start>,<step>..]}
\item infinite list with one element repeated: \ihaskell{[<element>,<element>..]}
\end{itemize}
To explicitly evaluate a finite list use the \ihaskell{init} function.
\begin{haskell}
-- all the following instructions are lazily evaluated
[1..10] -- => [1,2,3,4,5,6,7,8,9,10]
[1,3..10] -- => [1,3,5,7,9]
[1..] -- => [1,2,3,4,5,6,7,8,9,10,...]
[1,3..] -- => [1,3,5,7,9,...]
[1,1..] -- => [1,1,1,1,1,1,1,1,1,1,...]
\end{haskell}
\paragraph{List Comprehension}
\begin{itemize}
\item list comprehension returns a list of elements created by evaluation of the generators
\item syntax: \ihaskell{[<expression> | <generator>, <generator>, ...]}
\end{itemize}
\begin{haskell}
[x | x <- [1..10], even x] -- => [2,4,6,8,10]
[x * y | x <- [2,5], y <- [8,10]] -- => [16,20,40,50]
\end{haskell}
\subsection{Control flow}
\subsubsection{Pattern matching}
\begin{itemize}
\item the matching process is done top to bottom, left to right
\item patterns may have boolean guards
\item character \ihaskell{_} matches everything \textit{(don't care)}
\end{itemize}
\begin{haskell}
sign x | x > 0 = 1
| x < 0 = -1
| otherwise = 0
take 0 _ = []
take _ [] = []
take n (x:xs) = x : take (n - 1) xs
\end{haskell}
\breakcolumn
\subsubsection{Case}
\begin{itemize}
\item syntax: \ihaskell{case <value> of <pattern> -> <then> ...}
\item the \ihaskell{_} pattern matches everything
\end{itemize}
\begin{haskell}
case x of
0 -> "zero"
1 -> "one"
_ -> "other"
\end{haskell}
\subsubsection{Conditionals}
\begin{itemize}
\item if: \ihaskell{if <predicate> then <then> else <else>}
\item when: \ihaskell{when <predicate> <then>}
\item unless: \ihaskell{unless <predicate> <else>}
\end{itemize}
\begin{haskell}
if True then 1 else 2 -- => 1
when True 1 -- => 1
unless False 1 -- => 1
-- equivalent to
if True then 1 else 2 -- => 1
if True then 1 -- => 1
if False then 1 -- => ()