-
Notifications
You must be signed in to change notification settings - Fork 13
/
120.1.tex
909 lines (566 loc) · 22.4 KB
/
120.1.tex
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
\documentclass[twocolumn,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[landscape]{geometry}
\geometry{verbose,tmargin=0.5in,bmargin=0.75in,lmargin=0.5in,rmargin=0.5in}
\makeatletter
\setlength{\columnsep}{0.25in}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage{listings}
\lstset{
language=haskell,
tabsize=2,
basicstyle=\small\ttfamily,
}
\makeatother
\usepackage{babel}
\usepackage{listings}
\renewcommand{\lstlistingname}{Listing}
\begin{document}
\title{Reference Sheet for CO120.1 Programming I}
\date{Autumn 2016}
\maketitle
This reference sheet does not cover many interesting types and classes,
such as \texttt{Either}, \texttt{IO}, \texttt{Complex}, and \texttt{Monad}.
These are not included in the 120.1 syllabus, but even a limited understanding
of them may be helpful in examinations.
\section{Bools}
\begin{lstlisting}[language=Haskell,frame=single]
(&&) :: Bool -> Bool -> Bool
(||) :: Bool -> Bool -> Bool
not :: Bool -> Bool
\end{lstlisting}
\section{Maybes}
\begin{lstlisting}[language=Haskell,frame=single]
maybe :: b -> (a -> b) -> Maybe a -> b
\end{lstlisting}
Given default value, a function, and \texttt{Maybe} value: Returns
default value if \texttt{Maybe} value is \texttt{Nothing}; Otherwise
applies function to value inside \texttt{Just}.
\begin{lstlisting}[language=Haskell,frame=single]
isJust :: Maybe a -> Bool
isNothing :: Maybe a -> Bool
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.Maybe}}\emph{.}
\begin{lstlisting}[language=Haskell,frame=single]
fromJust :: Maybe a -> a
fromMaybe :: a-> Maybe a -> a
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.Maybe}}\emph{. }
\texttt{fromJust}: Given a \texttt{Maybe} value: Returns value inside
\texttt{Just} or error if \texttt{Nothing}.
\texttt{fromMaybe}: Given default value and \texttt{Maybe} value:
Returns value inside \texttt{Just} or default value if \texttt{Nothing}.
\begin{lstlisting}[language=Haskell,frame=single]
catMaybes :: [Maybe a] -> [a]
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.Maybe}}\emph{. }
\texttt{catMaybes}: Given list of \texttt{Maybe} values: Returns a
list of all \texttt{Just} values.
\texttt{mapMaybe}: Given function from value to \texttt{Maybe} value
and list of values: Returns a list of all \texttt{Just} values from
mapping function to list of values.
\section{Tuples}
\begin{lstlisting}[language=Haskell,frame=single]
fst :: (a, b) -> a
snd :: (a, b) -> b
\end{lstlisting}
It is usually better to use pattern matching unless used with higher
order functions.
\begin{lstlisting}[language=Haskell,frame=single]
curry :: ((a, b) -> c) -> a -> b -> c
uncurry :: a -> b -> c -> (a, b) -> c
\end{lstlisting}
\texttt{curry}: Given uncurried function $f(x,y)$: Returns curried
function $f\:x\:y$.
\texttt{uncurry}: Given curried function $f\:x\:y$: Returns uncurried
function $f(x,y)$.
\begin{lstlisting}[language=Haskell,frame=single]
swap :: (a,b) -> (b,a)
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.Tuple}}\emph{.}
\section{Enums}
\begin{lstlisting}[language=Haskell,frame=single]
succ :: a -> a
pred :: a -> a
\end{lstlisting}
\texttt{succ}: Given a value: Returns its successor.
\texttt{pred}: Given a value: Returns its predecessor.
\begin{lstlisting}[language=Haskell,frame=single]
[n..]
[n,n'..]
[n..m]
[n,n'..m]
\end{lstlisting}
Returns enum from $n$.
Returns enum from $n$ then $n'$.
Returns enum from $n$ to $m$.
Returns enum from $n$ then $n'$ to $m$.
\section{Numbers}
\subsection{Common Operators}
\begin{lstlisting}[language=Haskell,frame=single]
(+), (-), (*) :: Num a => a -> a -> a
(/) :: Fractional a => a -> a -> a
(^), (^^) :: (Fractional a, Integral b) => a -> b -> a
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
negate :: Num a => a -> a
recip :: Fractional a => a -> a
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
abs :: Num a => a -> a
signum :: Num a => a -> a
\end{lstlisting}
\texttt{abs}: Given a value, returns its absolute value.
\texttt{signum}: Given a value, returns its sign.
\subsection{Mathematical Constants and Functions}
\begin{lstlisting}[language=Haskell,frame=single]
pi :: Floating a => a
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
exp, log, sqrt :: Floating a => a -> a
(**), logBase :: Floating a => a -> a -> a
\end{lstlisting}
\texttt{log}: Natural log.
\begin{lstlisting}[language=Haskell,frame=single]
sin, cos, tan, asin, acos, atan :: Floating a => a -> a
\end{lstlisting}
In Radians.
\subsection{Number Theoretical Functions}
\begin{lstlisting}[language=Haskell,frame=single]
even :: Integral a => a -> Bool
odd :: Integral a => a -> Bool
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
div :: Num a => a -> a -> a
mod :: Num a => a -> a -> a
divMod :: Num a => a -> a -> (a, a)
\end{lstlisting}
\texttt{divMod}: Simultaneous div and mod.
\begin{lstlisting}[language=Haskell,frame=single]
gcd :: Integral a => a -> a -> a
lcm :: Integral a => a -> a -> a
\end{lstlisting}
\subsection{Conversion between Numerical Types}
\begin{lstlisting}[language=Haskell,frame=single]
round, ceiling, floor :: (Floating a, Num b) => a -> b
\end{lstlisting}
\texttt{round}: Rounds to the closest integer, or even integer if
closest integers are equidistant.
\begin{lstlisting}[language=Haskell,frame=single]
fromIntegral :: (Integral a, Num b) a -> b
\end{lstlisting}
\section{Characters}
\emph{Requires }\texttt{\emph{Data.Char}}\emph{.}
\subsection{Classification}
\begin{lstlisting}[language=Haskell,frame=single]
isSpace :: Char -> Bool
isLower :: Char -> Bool
isUpper :: Char -> Bool
isAlpha :: Char -> Bool
isAlphaNum :: Char -> Bool
isDigit :: Char -> Bool
isPunctuation :: Char -> Bool
isSeparator :: Char -> Bool
\end{lstlisting}
\subsection{Case Conversion}
\begin{lstlisting}[language=Haskell,frame=single]
toUpper :: Char -> Char
toLower :: Char -> Char
\end{lstlisting}
\subsection{Numeric Conversion}
\begin{lstlisting}[language=Haskell,frame=single]
ord :: Char -> Int
chr :: Int -> Char
\end{lstlisting}
\section{Lists}
\subsection{Working with Lists}
\subsubsection{List Operations and Transformations}
\begin{lstlisting}[language=Haskell,frame=single]
(:) :: a -> [a] -> [a]
(++) :: [a] -> [a] -> [a]
\end{lstlisting}
\texttt{(:)}: Cons: Adds single element to beginning of a list.
\texttt{(++)}: Appends two lists.
\begin{lstlisting}[language=Haskell,frame=single]
map :: (a -> b) -> [a] -> [b]
filter :: (a -> Bool) -> [a] -> [a]
\end{lstlisting}
\texttt{map}: Maps a function over a list. The more general function
\texttt{fmap} (infix \texttt{<\$>} works for all functor types).
\texttt{filter}: Returns a list of elements for which a predicate
holds.
\begin{lstlisting}[language=Haskell,frame=single]
head :: [a] -> a
last :: [a] -> a
tail :: [a] -> [a]
init :: [a] -> [a]
\end{lstlisting}
\texttt{head, last}: Returns the first, last element respectively.
\texttt{tail, init}: Returns all elements but the first, last respectively.
\begin{lstlisting}[language=Haskell,frame=single]
null :: [a] -> Bool
\end{lstlisting}
Returns \texttt{True} if and only if the given list is empty.
\begin{lstlisting}[language=Haskell,frame=single]
reverse :: [a] -> [a]
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
intersperse :: a -> [a] -> [a]
intercalate :: [a] -> [[a]] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.}
\texttt{intersperse}: Given an element and a list: Returns a list
with element interspersed between each element of the list.
\texttt{intercalate}: Intersperses elements between each list in a
set of lists.
\begin{lstlisting}[language=Haskell,frame=single]
transpose :: [[a]] -> [[a]]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} E.g. \texttt{transpose}
$[[1,2,3,4],[11,12],[1,2,13],[5]]=[[1,11,1,5],[2,12,2],[3,13],[4]]$
\begin{lstlisting}[language=Haskell,frame=single]
subsequences :: [a] -> [[a]]
permutations :: [a] -> [[a]]
\end{lstlisting}
\texttt{subsequences}: Returns a list of all subsequences.
\texttt{permutations}: Returns a list of all permutations.
\subsubsection{Sublists}
\begin{lstlisting}[language=Haskell,frame=single]
take :: Int -> [a] -> [a]
drop :: Int -> [a] -> [a]
splitAt :: Int -> [a] -> ([a], [a])
\end{lstlisting}
\texttt{splitAt n xs}: \texttt{take n xs} and \texttt{drop n xs}.
\begin{lstlisting}[language=Haskell,frame=single]
takeWhile :: (a -> Bool) -> [a] -> [a]
dropWhile :: (a -> Bool) -> [a] -> [a]
span :: (a -> Bool) -> [a] -> ([a], [a])
break :: (a -> Bool) -> [a] -> ([a], [a])
\end{lstlisting}
\textbf{Don't forget to use }\texttt{\textbf{takeWhile}}\textbf{ and
}\texttt{\textbf{dropWhile}}\textbf{!}
\texttt{span}: Given a predicate and a list: Returns a tuple where
the first element is the longest prefix of the list that satisfy the
predicate and the second element is the remainder.
\texttt{break}: As for \texttt{span}, but for the longest prefix that
does not satisfy the predicate.
\begin{lstlisting}[language=Haskell,frame=single]
dropWhileEnd :: (a -> Bool) -> [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{. }Drops the largest
suffix of the list for which the predicate holds.
\begin{lstlisting}[language=Haskell,frame=single]
stripPrefix :: [a] -> [a] -> Maybe [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{. }Drops the given
prefix from a list, or returns \texttt{Nothing} if the list did not
start with the given prefix.
\begin{lstlisting}[language=Haskell,frame=single]
group :: [a] -> [[a]]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{. }Given a list; Returns
a list of lists, that when concatenated reform the original list,
and such that each sublist only contains equal elements.
\begin{lstlisting}[language=Haskell,frame=single]
inits :: [a] -> [[a]]
tails :: [a] -> [[a]]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.}
\texttt{inits}: Returns all initial segments, shortest first.
\texttt{tails}: Returns all final segments, shortest first.
\subsubsection{Predicates}
\begin{lstlisting}[language=Haskell,frame=single]
isPrefixOf :: [a] -> [a] -> Bool
isSuffixOf :: [a] -> [a] -> Bool
isInfixOf :: [a] -> [a] -> Bool
isSubsequenceOf :: [a] -> [a] -> Bool
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.}
\texttt{isPrefixOf}: Returns \texttt{True} if the first list is a
prefix of the second.
\texttt{isSuffixOf}: Returns \texttt{True} if the first list is a
suffix of the second.
\texttt{isInfixOf}: Returns \texttt{True} if the first list is contained,
wholly and intact, in the second.
\texttt{isSubsequenceOf}: Returns \texttt{True} if the first list
is contained, in order, in the second (elements not necessarily consecutive).
\subsubsection{Indexing Lists}
\begin{lstlisting}[language=Haskell,frame=single]
(!!) :: [a] -> Int -> a
\end{lstlisting}
Returns element at given index.
\begin{lstlisting}[language=Haskell,frame=single]
elemIndex :: a -> [a] -> Maybe Int
elemIndices :: a -> [a] -> [Int]
findIndex :: (a -> Bool) -> [a] -> Maybe Int
findIndices :: (a -> Bool) -> [a] -> [Int]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.}
\texttt{elemIndex, findIndex}: Returns the index of the first element
in the given list that is equal to the given value, satisfies the
given predicate respectively; Or returns \texttt{Nothing} if none
exists.
\texttt{elemIndices, findIndices},: Returns the indices of all elements
in the given list that are equal to the given value, satisfy the given
predicate respectively.
\subsubsection{Searching Lists}
\begin{lstlisting}[language=Haskell,frame=single]
elem :: a -> [a] -> Bool
notElem :: a -> [a] -> Bool
\end{lstlisting}
\texttt{elem, notElem}: Returns \texttt{True} if the given element
is, is not an element of the given list respectively.
\begin{lstlisting}[language=Haskell,frame=single]
lookup :: a -> [(a, b)] -> Maybe b
\end{lstlisting}
Looks up a key in a dictionary; Returns \texttt{Nothing} if key not
found.
\begin{lstlisting}[language=Haskell,frame=single]
find :: (a -> Bool) -> [a] -> Maybe a
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Returns the first
element that satisfies the predicate, or \texttt{Nothing} if none
exists.
\begin{lstlisting}[language=Haskell,frame=single]
partition :: (a -> Bool) -> [a] -> ([a], [a])
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Returns the pair
of lists of elements which do and do not satisfy the predicate respectively.
\subsubsection{Zipping and Unzipping Lists}
\begin{lstlisting}[language=Haskell,frame=single]
zip :: [a] -> [b] -> [(a, b)]
zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
unzip :: [(a, b)] -> ([a], [b])
unzip3 :: [(a, b, c)] -> ([a], [b], [c])
\end{lstlisting}
\subsection{Building Lists}
\subsubsection{Building Lists}
\begin{lstlisting}[language=Haskell,frame=single]
scanr :: (a -> b -> b) -> b -> [a] -> [b]
scanl :: (b -> a -> b) -> b -> [a] -> [b]
scanr1, scanl1 :: (a -> a -> a) -> [a] -> [a]
\end{lstlisting}
Returns a list of successive values from respective fold function.
\begin{lstlisting}[language=Haskell,frame=single]
unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
\end{lstlisting}
Given a function that produces a \texttt{Maybe} pair and a starting
value; Applies the function to the starting value; If the function
returns a \texttt{Just} pair, the first element is added to the resulting
list and the function is applied again to the second element; If the
function returns \texttt{Nothing}, the resulting list is returned.
\begin{lstlisting}[language=Haskell,frame=single]
mapAccumR (or L) :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
\end{lstlisting}
Given a function to a pair of an accumulator and a result, and a starting
value for the accumulator; Returns a pair of the final accumulator
and the list of results.
\subsubsection{Infinite Lists}
\begin{lstlisting}[language=Haskell,frame=single]
iterate :: (a -> a) -> a -> [a]
repeat :: a -> [a]
replicate :: Int -> a -> [a]
cycle :: [a] -> [a]
\end{lstlisting}
\texttt{iterate}: Returns an infinite list of applications of a function
to a starting value.
\texttt{repeat}: Returns an infinite list with each element of the
given value.
\texttt{replicate}: Returns a list of given length of a repeated element.
\texttt{cycle}: Returns an infinite list produced by cycling the given
list.
\subsection{Reducing Lists}
\subsubsection{Folds}
\begin{lstlisting}[language=Haskell,frame=single]
foldr :: (a -> b -> b) -> b -> [a] -> b
foldl :: (b -> a -> b) -> b -> [a] -> b
foldr1 :: (a -> a -> a) -> [a] -> a
foldl1 :: (a -> a -> a) -> [a] -> a
\end{lstlisting}
\textbf{Don't forget to use folds!}
Given a binary operator, starting value and list: Reduces list using
the binary operator in the given direction, starting from the given
starting value.
e.g. \texttt{foldr} $f$ $x_{0}$ $[x_{1},x_{2},x_{3},\dots,x_{n}]$
$=$ $f$ $x_{1}(f$ $x_{2}(f$ $x_{3}($ $\dots$ $(f$ $x_{n}$
$x_{0}))))$,
and \texttt{foldl} $f$ $x_{0}$ $[x_{1},x_{2},x_{3},\dots,x_{n}]$
$=$ $(f$ $(f$ $(f$ $(f$ $x_{0}$ $x_{1})$ $x_{2})$ $x_{3})$$\dots$
$x_{n})$.
\texttt{fold1}: Has no starting value.
\subsubsection{Special Folds}
\begin{lstlisting}[language=Haskell,frame=single]
length :: [a] -> Int
sum :: Num a => [a] -> a
product :: Num a => [a] -> a
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
maximum :: [a] -> a
minimum :: [a] -> a
\end{lstlisting}
\begin{lstlisting}[language=Haskell,frame=single]
or :: [Bool] -> Bool
and :: [Bool] -> Bool
any :: (a -> Bool) -> [a] -> Bool
all :: (a -> Bool) -> [a] -> Bool
\end{lstlisting}
\texttt{or, any}: Returns \texttt{True} if \texttt{True} for any element.
\texttt{and, all}: Returns \texttt{True} if \texttt{True} for all
elements.
\begin{lstlisting}[language=Haskell,frame=single]
concat :: [[a]] -> [a]
concatMap :: (a -> [b]) -> [a] -> b
\end{lstlisting}
\texttt{concat}: Concatenates a list of lists into a list.
\texttt{concatMap}: Maps a function, then concatenates result.
\subsection{Special Lists}
\subsubsection{Set Functions}
\begin{lstlisting}[language=Haskell,frame=single]
nub :: [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Removes duplicates.
\begin{lstlisting}[language=Haskell,frame=single]
delete :: a -> [a] -> [a]
(\\) :: [a] -> [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Deletes first occurrence
of given element, each element of given list.
\begin{lstlisting}[language=Haskell,frame=single]
union :: [a] -> [a] -> [a]
intersect :: [a] -> [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Returns union, intersection
of two lists.
\subsubsection{Ordered Lists}
\begin{lstlisting}[language=Haskell,frame=single]
sort :: [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Sorts list.
\begin{lstlisting}[language=Haskell,frame=single]
sortOn :: (a -> b) -> [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Sorts list by comparing
values generated by given function.
\begin{lstlisting}[language=Haskell,frame=single]
insert :: a -> [a] -> [a]
\end{lstlisting}
\emph{Requires }\texttt{\emph{Data.List}}\emph{.} Inserts element
so that a sorted list remains sorted.
\subsubsection{Functions on Strings}
\begin{lstlisting}[language=Haskell,frame=single]
lines :: String -> [String]
words :: String -> [String]
unlines :: [String] -> String
unwords :: [String] -> String
\end{lstlisting}
\texttt{words}: Breaks up into a list of words, delimited by white
space.
\texttt{lines}: Breaks up into a list of strings separated by newlines.
\section{Functions}
\begin{lstlisting}[language=Haskell,frame=single]
id :: a -> a
const :: a -> b -> a
asTypeOf :: a -> a -> a
\end{lstlisting}
\texttt{id}: Identity function.
\texttt{const}: Evaluates to constant value for all inputs.
\texttt{asTypeOf}: Forces first argument to have the same type as
the second.
\begin{lstlisting}[language=Haskell,frame=single]
(.) :: (b -> c) -> (a -> b) -> a -> c
($) :: (a -> b) -> a -> b
\end{lstlisting}
\texttt{(.)}: Function composition: $(f.g)\:x=f(g\:x)$. Note the
requirements for the type and number of arguments. You can use this
to combine two higher order functions into one.
\texttt{(\$)}: Function application: $f\:\$\:g\:\$\:h\:x=f(g(h\:x))$.
Often used to omit parentheses. To be avoided in general!
\begin{lstlisting}[language=Haskell,frame=single]
flip :: (a -> b -> c) -> b -> a -> c
\end{lstlisting}
Use to invert arguments. Alternative to back-quotes or lambdas.
\begin{lstlisting}[language=Haskell,frame=single]
until :: (a -> Bool) -> (a -> a) -> a -> a
\end{lstlisting}
Given a predicate and function; Applies function until predicate holds.
\begin{lstlisting}[language=Haskell,frame=single]
error "Error string"
\end{lstlisting}
Stops execution and displays error message.
\section{Types and Common Type Classes}
Use \texttt{data} to define a new data-type (you \emph{must} define
type constructors), \texttt{type} to define a type synonym, \texttt{class}
to define a new type-class, and \texttt{instance} to make a type an
instance of a type-class.
Don't forget you can use \texttt{deriving} in most cases. Example:
\begin{lstlisting}[language=Haskell,frame=single]
data Tree a = Empty | Leaf a | Node a (Tree a) (Tree a)
deriving (Show)
\end{lstlisting}
Remember you can use \texttt{:i} in ghci for information about types
and type-classes.
\section{Syntactic Features and Good Practice}
\paragraph{Spacing}
Use proper spacing and alignment. Keep lines short.
\paragraph{Function Names}
Give functions meaningful names. Don't ever give two different things
the same name!
\paragraph{Comments}
Use to keep code ordered and to explain complicated functions. Begin
with \texttt{-{}-}. Multli-line comments use \texttt{\{-} and \texttt{-\}}.
\paragraph{Multi-Line Strings}
End and start each line with \texttt{\textbackslash{}}.
\paragraph{Where clauses}
Use instead of \texttt{let} statements to avoid computing something
multiple times or to clean up code.
\paragraph{Helper Functions}
Use helper functions to provide complicated functionality (e.g. creating
a list \emph{and} checking for convergence as it is created). Accumulating
parameters are also particularly useful:
\begin{lstlisting}[language=Haskell,frame=single]
function input = function' input startingAcc
where
function' baseCase acc = acc
function' otherCase acc = function nextCase updatedAcc
\end{lstlisting}
\paragraph{Lambdas}
Use as an alternative to higher-order functions for very complicated
functions.
\paragraph*{List comprehensions}
Often functions involving lists can be written using comprehensions,
recursion, or higher-order functions. Choose carefully.
\begin{lstlisting}[language=Haskell,frame=single]
[f x | x <- xs, p] = map f $ filter p xs
\end{lstlisting}
\paragraph{Pattern matching}
Use often. Especially helpful for breaking up lists using \texttt{:}
and for working with tuples. Don't forget to use \texttt{\_} and \texttt{@}
where appropriate.
\paragraph{Guards}
Use instead of \texttt{if} ... \texttt{then} ... \texttt{else} statements.
\paragraph*{Tips for Lexis Tests}
\begin{itemize}
\item Read instructions carefully. Don't rewrite a given function!
\item Be prepared to write very little for the first questions and much
more (helper functions, etc.) towards the end. You should check your
answers to the first parts before beginning the last part. You should
spend time planning your answer (on paper) for the last part.
\item Aim firstly to get a working implementation, then consider optimisations.
\item Don't forget to use functions defined earlier in the program.
\item Test often (using \texttt{undefined} where necessary to allow compilation)
and read any error messages carefully.
\item Make use of any provided test cases but do not rely upon them.
\item Use an editor with syntax highlighting and \texttt{ghci} in terminal.
\item Use \texttt{:set -W} to turn on warnings and \texttt{:set -w} to turn
off warnings in \texttt{ghci}. Use \texttt{:browse} to see a list of all functions included in a module.
\end{itemize}
\end{document}