\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{jenwwnewnw} \pdfinfo{ /Title (cs18941.pdf) /Creator (Cheatography) /Author (jenwwnewnw) /Subject (442 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{B3D4A3} \definecolor{LightBackground}{HTML}{F5F9F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{442 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{jenwwnewnw} via \textcolor{DarkBackground}{\uline{cheatography.com/77170/cs/18941/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}jenwwnewnw \\ \uline{cheatography.com/jenwwnewnw} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 22nd April, 2021.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Syntax}} \tn % Row 0 \SetRowColor{LightBackground} `null {[}{]}` & return {\emph{True}} if list is empty \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `'H' {\bf{\textbackslash{}`elem\textbackslash{}`}} "Hello"` & return {\emph{True}} if H is in the string \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `head {[}1,2,3{]}` & return 1 \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `tail {[}1,2,3{]}` & return {[}2,3{]} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} `last {[}1,2,3{]}` & return 3 \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `init {[}1,2,3{]}` & return {[}1,2{]} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} `:t` & return the type \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `fst (5,2)` & return 5 \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} `snd (5,2)` & return 2 \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} `1:2:3:{[}{]}` & same as {[}1,2,3{]} \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} `length {[}{]}` & give length of list \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} `reverse {[}{]}` & reverse the list \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} `{[}{]} !! n` & gives the {\bf{n}}th element \tn % Row Count 16 (+ 2) % Row 13 \SetRowColor{white} `filter {\emph{test}} {[}{]}` & return everything that passes the test \tn % Row Count 18 (+ 2) % Row 14 \SetRowColor{LightBackground} `{[}{]} ++ {[}{]}` & list concatenation \tn % Row Count 19 (+ 1) % Row 15 \SetRowColor{white} `{[}{]} : {[}{]}` & list concatenation \tn % Row Count 20 (+ 1) % Row 16 \SetRowColor{LightBackground} `drop n {[}{]}` & delete the first {\bf{n}} element from list \tn % Row Count 22 (+ 2) % Row 17 \SetRowColor{white} `take n {[}{]}` & make a new list containing just the first N element \tn % Row Count 25 (+ 3) % Row 18 \SetRowColor{LightBackground} `splitAt n {[}{]}` & split list into two lists at {\bf{n}}th position \tn % Row Count 27 (+ 2) % Row 19 \SetRowColor{white} `zip {[}a..{]} {[}0...{]}` & combine tow list into tuples {[}(a,0{]}..{]} \tn % Row Count 29 (+ 2) % Row 20 \SetRowColor{LightBackground} `map {\emph{function}} {[}{[}{]}` & apply a function to all list elements \tn % Row Count 31 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Terminology}} \tn % Row 0 \SetRowColor{LightBackground} Polymorphic Types & Families of types. For example, (forall a){[}a{]} is the family of types consisting of, for every type a, the type of lists of a. Lists of integers (e.g. {[}1,2,3{]}), lists of characters ({[}'a','b','c'{]}), even lists of lists of integers, etc., are all members of this family. \tn % Row Count 12 (+ 12) % Row 1 \SetRowColor{white} Type Variable & Lower case, can be of any type. e.g. fst::({\bf{a}},{\bf{b}})-\textgreater{}{\bf{a}} \tn % Row Count 15 (+ 3) % Row 2 \SetRowColor{LightBackground} Typeclass & A sort of interface that defines some behavior. Basic type classes: Read, Show, Ord, Eq, Enum, Num. {\bf{Num}} includes {\emph{Int, Integer, Float, Double.}} \tn % Row Count 22 (+ 7) % Row 3 \SetRowColor{white} Higher-ordered Functions & A function that takes other functions as arguments or returns a function as result. Ex: foldl, folder,zipWith, flip. \tn % Row Count 27 (+ 5) % Row 4 \SetRowColor{LightBackground} Module & A collection of related functions, types and typeclasses \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Terminology (cont)}} \tn % Row 5 \SetRowColor{LightBackground} Referential Transparency & An expression is called referentially transparent if it can be replaced with its corresponding value without changing the program's behavior. \tn % Row Count 6 (+ 6) % Row 6 \SetRowColor{white} & {\bf{substituting equals for equals, different from other programing languages}} \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Type Signatures}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{In type signature, specific {\emph{(String)}} and general {\emph{(a,b)}} types can be mixed and matched.}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `concat3::String-\textgreater{}String-\textgreater{}String-\textgreater{}String` & `concat3 x y z = x++y++z` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `const :: a-\textgreater{}b-\textgreater{}a` & `const x y = x` \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `allEqual :: (Eq a) =\textgreater{} a -\textgreater{} a -\textgreater{} a -\textgreater{} Bool` & `allEqual x y z = x == y \&\& y == z` \tn % Row Count 8 (+ 3) % Row 4 \SetRowColor{LightBackground} `(.)::(b-\textgreater{}c)-\textgreater{}(a-\textgreater{}b)-\textgreater{}a-\textgreater{}c` & `f.g = \textbackslash{}x-\textgreater{} f (g x)` \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} & `(\textbackslash{}x-\textgreater{}10+x)5` \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Lambda function, lead with \textbackslash{}, then arguments, then -\textgreater{}, then the computation} \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Data Types}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Haskell uses various data types, all of them starts by a capital letter: \newline % Row Count 2 (+ 2) -{\bf{Int}}: Integer number with fixed precision \newline % Row Count 3 (+ 1) -{\bf{Integer}}: Integer number with virtually no limits \newline % Row Count 5 (+ 2) -{\bf{Float}}: Floating number \newline % Row Count 6 (+ 1) -{\bf{Bool}}: Boolean. Takes two values: True or False. \newline % Row Count 8 (+ 2) -{\bf{Char}}: Character. Any character in the code is placed between quotes ('). \newline % Row Count 10 (+ 2) -{\bf{String}}: Strings (In fact, a list of Chars).% Row Count 11 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.9954 cm} x{3.9816 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Properties of Haskell}} \tn % Row 0 \SetRowColor{LightBackground} Pure & No side effects in functions and expressions \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & No assignment operators such as ++ and =+ \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} & {\emph{I/O is an exception}} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} & Promotes referential transparency \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} & Once x is assigned to a value, the value stays \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{Functional} & Use recursion instead of iteration \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} & Allows operations on functions \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} Lazy & Don't do an operation unless you need the result. \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Recursive Descent Parser}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{-{}- our parsers generally are of type Parser {[}Ptree{]} \newline data Ptree = VAR String | ID String | FCN String {[}Ptree{]} \newline deriving (Show, Eq, Read) \newline \newline data Presult a = FAIL | OK a String deriving (Show, Eq, Read) \newline type Parser a = String -\textgreater{} Presult a \newline \newline -{}- As before, we use \&\textgreater{} and |\textgreater{} as AND / OR combinators on parsers \newline \newline expr = variable |\textgreater{} fcnCall |\textgreater{} identifier \newline fcnCall = buildCall . (identifier \&\textgreater{} skip "(" \&\textgreater{} arguments \&\textgreater{} skip ")") \newline \newline arguments = expr \&\textgreater{} argTail |\textgreater{} empty \newline argTail = skip "," \&\textgreater{} expr \&\textgreater{} argTail |\textgreater{} empty \newline \newline identifier input = beginsWith ID Data.Char.isLower isTailChar (dropblank input) \newline variable input = beginsWith VAR Data.Char.isUpper isTailChar (dropblank input) \newline \newline empty = OK {[}{]} -{}- empty string parser always succeeds \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline -{}- UTILITY ROUTINES \newline -{}- Parse a string but don't save it as a parse tree \newline skip :: String -\textgreater{} Parser {[}a{]} \newline skip want input = \newline let found = take (length want) input \newline remainder = dropblank (drop (length want) input) \newline in \newline if want == found then OK {[}{]} remainder \newline else FAIL \newline \newline -{}- Build a singleton list of a function call parse tree from a list with \newline -{}- an identifier followed by list of arguments \newline buildCall :: Presult {[}Ptree{]} -\textgreater{} Presult {[}Ptree{]} \newline buildCall FAIL = FAIL \newline buildCall (OK {[}{]} \_) = FAIL \newline buildCall (OK (ID fcn : args) remainder) = OK {[}FCN fcn args{]} remainder \newline -{}- Build a singleton list of a parse tree given the kind of tree we want \newline -{}- and the kinds of head and tail characters we want \newline beginsWith :: (String -\textgreater{} Ptree) -\textgreater{} (Char -\textgreater{} Bool) -\textgreater{} (Char -\textgreater{} Bool) -\textgreater{} Parser {[}Ptree{]} \newline beginsWith \_ \_ \_ "" = FAIL \newline beginsWith builder isHead isTail (c:cs) \newline | isHead c = let tail = Data.List.takeWhile isTail cs \newline in OK {[}builder (c:tail){]} (dropblank (drop (length tail) cs)) \newline | otherwise = FAIL \newline \newline -{}- Remove spaces (and tabs and newlines) from head of string. \newline -{}- \newline dropblank :: String -\textgreater{} String \newline dropblank = Data.List.dropWhile Data.Char.isSpace \newline \newline -{}- kind of character that makes up 2nd - end character of an id or var \newline -{}- \newline isTailChar :: Char -\textgreater{} Bool \newline isTailChar c = Data.Char.isAlphaNum c || c == '\_' \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline -{}- Concatenation and alternation operators on parsers \newline -{}- (|\textgreater{}) is an OR/Alternation operator for parsers. \newline -{}- \newline infixr 2 |\textgreater{} \newline (|\textgreater{}) :: Parser a -\textgreater{} Parser a -\textgreater{} Parser a \newline (p1 |\textgreater{} p2) input = \newline case p1 input of \newline m1 @ (OK \_ \_) -\textgreater{} m1 -{}- if p1 succeeds, just return what it did \newline FAIL -\textgreater{} p2 input \newline \newline -{}- (\&\textgreater{}) is an AND/Concatenation operator for parsers \newline infixr 3 \&\textgreater{} \newline (\&\textgreater{}) :: Parser {[}a{]} -\textgreater{} Parser {[}a{]} -\textgreater{} Parser {[}a{]} \newline (p1 \&\textgreater{} p2) input = \newline case p1 input of \newline FAIL -\textgreater{} FAIL -{}- p1 fails? we fail \newline OK ptrees1 remain1 -\textgreater{} \newline case p2 remain1 of -{}- run p2 on remaining input \newline FAIL -\textgreater{} FAIL -{}- p2 fails? we fail \newline OK ptrees2 remain2 -\textgreater{} -{}- both succeeded \newline OK (ptrees1 ++ ptrees2) remain2} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Tree}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`data Tree a = Leaf a | Branch a (Tree a) (Tree a) deriving (Eq, Show)` \newline % Row Count 2 (+ 2) `treeEq :: (Eq a) =\textgreater{} Tree a -\textgreater{} Tree a -\textgreater{} Bool` \newline % Row Count 3 (+ 1) `treeEq (Leaf x) (Leaf y) = x == y` \newline % Row Count 4 (+ 1) `treeEq (Branch x1 l1 r1) (Branch x2 l2 r2) = x1 == x2 \&\& treeEq l1 l2 \&\& treeEq r1 r2` \newline % Row Count 6 (+ 2) `treeEq \_ \_ = False` \newline % Row Count 7 (+ 1) treeShow \newline % Row Count 8 (+ 1) `treeShow :: Show a =\textgreater{} Tree a -\textgreater{} {[}Char{]}` \newline % Row Count 9 (+ 1) `treeShow (Leaf x) = "(Leaf " ++ show x ++ ")"` \newline % Row Count 11 (+ 2) `treeShow (Branch x left right)= "(Branch " ++ show x ++ " "++ treeShow left ++ " "++ treeShow right ++ ")"` \newline % Row Count 14 (+ 3) Preorder via standard recursion \newline % Row Count 15 (+ 1) `preorder :: Tree a -\textgreater{} {[}a{]}` \newline % Row Count 16 (+ 1) `preorder (Leaf x) = {[}x{]}` \newline % Row Count 17 (+ 1) `preorder (Branch x left right)= x : preorder left ++ preorder right` \newline % Row Count 19 (+ 2) Tail-recursive traversal \newline % Row Count 20 (+ 1) `preorder' :: Tree a -\textgreater{} {[}a{]} -\textgreater{} {[}a{]}` \newline % Row Count 21 (+ 1) `preorder' (Leaf x) xs = x : xs` \newline % Row Count 22 (+ 1) `preorder' (Branch r left right) xs= r : preorder' left (preorder' right xs)`% Row Count 24 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Syntax}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{addFour w x y z = \newline {\bf{let}} a = w + x \newline b = y + a \newline {\bf{in}} z + b \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline addFour w x y z = \newline z + b \newline {\bf{where}} \newline a = w + x \newline b = y + a \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline fib n \newline | n \textless{} 2 = 1 \newline | {\bf{otherwise}} = fib (n - 1) + fib (n - 2) \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline fib n = \newline {\bf{case}} n {\bf{of}} \newline 0 -\textgreater{} 1 \newline 1 -\textgreater{} 1 \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline fib n = \newline {\bf{if}} n \textless{} 2 \newline {\bf{then}} 1 \newline {\bf{else}} fib (n - 1) + fib (n - 2) \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline nameReturn :: IO String \newline nameReturn = {\bf{do}} putStr "What is your name? " \newline name \textless{}- getLine \newline {\bf{putStrLn}} ("Pleased to meet you, " ++ name ++ "!") \newline return full} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4177 cm} x{1.71257 cm} p{0.4177 cm} x{1.62903 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{5.377cm}}{\bf\textcolor{white}{Regex}} \tn % Row 0 \SetRowColor{LightBackground} . & Any character except new line (\textbackslash{}n) & & \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \textbackslash{}w & Word & * & 0 or more \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \textbackslash{}S & Not white space & + & 1 or more \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \textbackslash{}s & White space & ? & 0 or 1 \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \textbackslash{}W & Not word & \{3\} & Exactly 3 \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \textbackslash{}d & Digit & \{3,\} & 3 or more \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \textbackslash{}D & Not digit & \{3,5\} & 3, 4 or 5 \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} \textbackslash{}b & Word boundary & \textasciicircum{} & Beginning of String \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} \textbackslash{}B & Not word boundary & \$ & End of String \tn % Row Count 14 (+ 2) % Row 9 \SetRowColor{white} {[}\textasciicircum{} {]} & matches characters NOT in bracket & {[} {]} & matches characters in brackets \tn % Row Count 17 (+ 3) % Row 10 \SetRowColor{LightBackground} | & Either Or & ( ) & Group \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} ε & Empty string containing no characters & & \tn % Row Count 21 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{5.377cm}}{\{\{ac\}\} \textasciicircum{} {[} . \$ \{ * ( \textbackslash{} + ) | ? \textless{} \textgreater{} \newline Matecharacters need to be escaped} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Currying}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Currying}} is the process of transforming a function that takes multiple arguments in a tuple as its argument, into a function that takes just a single argument and returns another function which accepts further arguments, one by one, that the original function would receive in the rest of that tuple.} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{from `g :: (a, b) -\textgreater{} c` to `f :: a -\textgreater{} (b -\textgreater{} c) `} \tn % Row Count 9 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`f :: a -\textgreater{} (b -\textgreater{} c) ` is the same as `f :: a -\textgreater{} b -\textgreater{} c `} \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{` g (x,y) = x + y ` is an uncurried function, has the type `g :: Num a =\textgreater{} (a, a) -\textgreater{} a`} \tn % Row Count 13 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{` h x y = x + y` is a curried addition, has the type `h :: Num c =\textgreater{} c -\textgreater{} c -\textgreater{} c`} \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`curry g` can convert it to a curried function} \tn % Row Count 16 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Fold List}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Foldl}} takes a binary operation, a starting value, and the list to fold} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`foldl (-) 0 {[}3,5,8{]}` =\textgreater{} ` (((0 - 3) - 5) - 8)` =\textgreater{} `-16`} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{foldl and foldr is under the type class {\bf{ `Foldable` }}} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`foldl :: Foldable t =\textgreater{} (b -\textgreater{} a -\textgreater{} b) -\textgreater{} b -\textgreater{} t a -\textgreater{} b`} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`foldr :: Foldable t =\textgreater{} (a -\textgreater{} b -\textgreater{} b) -\textgreater{} b -\textgreater{} t a -\textgreater{} b`} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`elem' y ys = foldl (\textbackslash{}acc x -\textgreater{} if x == y then True else acc) False ys`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Notes}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`head\_repeats n x = (take n x) == (take n (drop n x))` \newline % Row Count 2 (+ 2) returns True if the first n elements of x equals the second n elements of x.If n ≤ 0, return True. \newline % Row Count 5 (+ 3) -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline % Row Count 6 (+ 1) `swap\_ends {[}{]} = {[}{]}` \newline % Row Count 7 (+ 1) `swap\_ends {[}y{]} = {[}y{]}` \newline % Row Count 8 (+ 1) `swap\_ends x = last x : (reverse (drop 1 (reverse (drop 1 x))))++ {[}head x{]}` \newline % Row Count 10 (+ 2) Define a function swap\_ends that takes a list and returns the same list but with the first and last elements swapped. \newline % Row Count 13 (+ 3) -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline % Row Count 14 (+ 1) iterate via standard recursion \newline % Row Count 15 (+ 1) `iterate1 n f` \newline % Row Count 16 (+ 1) ` | n \textless{}= 0 = id` \newline % Row Count 17 (+ 1) `| otherwise = f . (iterate1 (n-1) f)` \newline % Row Count 19 (+ 2) iterate via foldl \newline % Row Count 20 (+ 1) `iterate2 n f = foldl (.) id {[}f | i \textless{}- {[}1..n{]}{]}` \newline % Row Count 21 (+ 1) -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline % Row Count 22 (+ 1) `f1a :: (b, a) -\textgreater{} (a, b)` \newline % Row Count 23 (+ 1) `f1a = \textbackslash{}(x, y) -\textgreater{} (y, x)` \newline % Row Count 24 (+ 1) `f1b :: a -\textgreater{} {[}a{]} -\textgreater{} {[}{[}a{]}{]}` \newline % Row Count 25 (+ 1) `f1b = \textbackslash{}x y -\textgreater{} {[}{[}x{]}, y{]}` \newline % Row Count 26 (+ 1) `f1c :: a -\textgreater{} a -\textgreater{} {[}a{]} -\textgreater{} {[}{[}a{]}{]}` \newline % Row Count 27 (+ 1) `f1c = \textbackslash{}x y z -\textgreater{} {[}x : z, y : z{]}` \newline % Row Count 28 (+ 1) `f1d :: (a -\textgreater{} Bool) -\textgreater{} {[}a{]} -\textgreater{} Int` \newline % Row Count 29 (+ 1) `f1d f = length . (filter f)`% Row Count 30 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`(:) :: a -\textgreater{} {[}a{]} -\textgreater{} {[}a{]}` \newline `(++) :: {[}a{]} -\textgreater{} {[}a{]} -\textgreater{} {[}a{]}` \newline {\emph{`++` is only used for list concatenation, whereas `:` is used for joining element with lists}} \newline {\emph{Num class does not support {\bf{ /}}, Fractional does}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Pattern Matching}} \tn % Row 0 \SetRowColor{LightBackground} `(x:xs)` & head x and tail xs \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `(x:3:xs)` & list where 2nd element is 3 \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `myData a \_ c` & ignore one of the component \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\emph{ `data Pattern a = P a | POr (Pattern a) (Pattern a)| PAnd (Pattern a) (Pattern a) deriving Show`}}} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`match pattern {[}{]} = (False, {[}{]})` \newline `match (P x) (y : ys) = if x == y then (True, ys) else (False, y : ys)` \newline {\bf{`match (POr pat1 pat2) xs}} =case match pat1 xs of` \newline `(True, leftover) -\textgreater{} (True, leftover)` \newline `(False, \_) -\textgreater{} match pat2 xs` \newline {\bf{`match (PAnd pat1 pat2) xs}} =case match pat1 xs of ` \newline ` (False, \_) -\textgreater{} (False, xs)` \newline `(True, leftover) -\textgreater{}case match pat2 leftover of ` \newline `(False, \_) -\textgreater{} (False, xs)` \newline `(True, leftover2) -\textgreater{} (True, leftover2)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Regex Examples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`Natural numbers with no leading zeros except just 0`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{literal\}\} 0 | {[}1-9{]} \textbackslash{}d*} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`Floating point numbers w/o leading zeros`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{literal\}\}(0 | {[}1-9{]} \textbackslash{}d{\emph{.\textbackslash{}d}} | . \textbackslash{}d+)?({[}eE{]}{[}+-{]}?{[}0-9{]}+))} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`Hex numbers allowing leading zeros`} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{literal\}\}0x{[}0-9a-fA-F{]}+} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`Strings with an even \#a's or number ofb's divisible by 2`} \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{literal\}\}(b{\emph{ab}}a){\emph{b}}|(a{\emph{ba}}ba{\emph{b)}}a*} \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Match regular expressions using backtracking}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{data RegExp = Rnull \newline | Rend \newline | Rany \newline | Rch Char \newline | Ror RegExp RegExp \newline | Rand RegExp RegExp \newline | Ropt RegExp \newline | Rstar RegExp \newline deriving (Eq, Show) \newline data Mresult = FAIL | OK String String deriving (Eq, Show) \newline match :: RegExp -\textgreater{} String -\textgreater{} Mresult \newline match Rnull str = OK "" str \newline match Rend "" = OK "" "" \newline match Rend str = FAIL \newline \newline match Rany "" = FAIL \newline match Rany (c : cs) = OK {[}c{]} cs \newline \newline match (Rch ch1) "" = FAIL \newline match (Rch ch1) (str @ (ch2 : left)) \newline | ch1 == ch2 = OK {[}ch1{]} left \newline | otherwise = FAIL \newline match (Ror exp1 exp2) str = \newline case match exp1 str of \newline FAIL -\textgreater{} match exp2 str \newline result1 @ (OK match1 remain1) -\textgreater{} \newline case match exp2 str of \newline FAIL -\textgreater{} result1 \newline result2 @ (OK match2 remain2) -\textgreater{} \newline if length match1 \textgreater{}= length match2 \newline \newline match (Rand exp1 exp2) str = \newline case match exp1 str of \newline FAIL -\textgreater{} FAIL \newline ok @ (OK match1 remain1) -\textgreater{} \newline extend match1 (match exp2 remain1) \newline \newline match (Ropt exp) str = match (Ror exp Rnull) str \newline \newline match (Rstar exp) str = \newline case match exp str of \newline FAIL -\textgreater{} OK "" str \newline OK match1 remain1 -\textgreater{} \newline if match1 == "" then OK "" str \newline else \newline extend match1 (match (Ror (Rstar exp) Rnull) remain1) \newline \newline extend match1 (OK match2 remain2) = OK (match1 ++ match2) remain2 \newline extend match1 FAIL = FAIL \newline \newline -{}- mkAnd string = the exp that matches each character of the string in sequence. \newline -{}- \newline mkAnd (c : "") = Rch c \newline mkAnd (c : cs) = Rand (Rch c) (mkAnd cs) \newline -{}- \newline mkOr (c : "") = Rch c \newline mkOr (c : cs) = Ror (Rch c) (mkOr cs)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{More Examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(Find out whether a list is a palindrome) \newline isPalindrome'' :: (Eq a) =\textgreater{} {[}a{]} -\textgreater{} Bool \newline isPalindrome'' xs = foldl (\textbackslash{}acc (a,b) -\textgreater{} if a == b then acc else False) True input where input = zip xs (reverse xs) \newline (Eliminate consecutive duplicates of list elements) \newline compress :: Eq a =\textgreater{} {[}a{]} -\textgreater{} {[}a{]} \newline compress = map head . group \newline (Count the leaves of a binary tree) \newline countLeaves Empty = 0 \newline countLeaves (Branch \_ Empty Empty) = 1 \newline countLeaves (Branch \_ left right) = countLeaves left+ countLeaves right \newline (User-Defined Polymorphic Lists) \newline (a) Define the function foldList which acts on user-defined lists just as foldr acts on native lists. \newline foldList :: (a -\textgreater{} b -\textgreater{} b) -\textgreater{} b -\textgreater{} List a -\textgreater{} b \newline foldList f init Nil = init \newline foldList f init (Cons x xs) = f x (foldList f init xs) \newline (b) Define the function sumList which adds up the entries in an argument of type (List Int). \newline sumList :: (List Int) -\textgreater{} Int \newline sumList = foldList (+) 0} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Lecture 11}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{data ParseT = STR String | LIST {[}ParseT{]} deriving (Show, Eq, Read) \newline data PResult = FAIL | OK {[}ParseT{]} String deriving (Show, Eq, Read) \newline \newline type Parser = String -\textgreater{} PResult \newline type TreeBuilder = {[}ParseT{]} -\textgreater{} ParseT -{}- LIST, for these trees \newline \newline -{}- Note use of \&\textgreater{} as AND and |\textgreater{} as OR \newline list = parse LIST (skip "(" \&\textgreater{} list \&\textgreater{} sublist \&\textgreater{} skip ")" \newline |\textgreater{} skip "{[}" \&\textgreater{} list \&\textgreater{} sublist \&\textgreater{} skip "{]}" \newline |\textgreater{} identifier) \newline \newline sublist = (skip ",") \&\textgreater{} list \&\textgreater{} sublist |\textgreater{} empty \newline identifier = literal "x" \newline empty = OK {[}{]} -{}- empty string parser always succeeds \newline -{}- expr = expr \&\textgreater{} literal "+" \&\textgreater{} identifier |\textgreater{} empty \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline -{}- UTILITY ROUTINES \newline -{}- Parse a string and make it a parse tree \newline literal :: String -\textgreater{} Parser \newline literal want input = \newline let found = take (length want) input \newline remainder = dropblank (drop (length want) input) \newline in \newline if want == found then OK {[}STR want{]} remainder \newline else FAIL \newline -{}- Parse a string but don't save it as a parse tree \newline skip want input = \newline case literal want input of \newline FAIL -\textgreater{} FAIL \newline OK \_ remain -\textgreater{} OK {[}{]} remain \newline -{}- Remove spaces from head of string \newline dropblank = Data.List.dropWhile Data.Char.isSpace \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline -{}- Concatenation and alternation operators on parsers \newline -{}- (|\textgreater{}) is an OR/Alternation operator for parsers. \newline -{}- \newline infixr 2 |\textgreater{} \newline (|\textgreater{}) :: Parser -\textgreater{} Parser -\textgreater{} Parser \newline (p1 |\textgreater{} p2) input = \newline case p1 input of \newline m1 @ (OK \_ \_) -\textgreater{} m1 -{}- if p1 succeeds, just return what it did \newline FAIL -\textgreater{} p2 input \newline \newline -{}- (\&\textgreater{}) is an AND/Concatenation operator for parsers \newline -{}- \newline infixr 3 \&\textgreater{} \newline (\&\textgreater{}) :: Parser -\textgreater{} Parser -\textgreater{} Parser \newline (p1 \&\textgreater{} p2) input = \newline case p1 input of \newline FAIL -\textgreater{} FAIL -{}- p1 fails? we fail \newline OK ptrees1 remain1 -\textgreater{} \newline case p2 remain1 of -{}- run p2 on remaining input \newline FAIL -\textgreater{} FAIL -{}- p2 fails? we fail \newline OK ptrees2 remain2 -\textgreater{} -{}- both succeeded \newline OK (ptrees1 ++ ptrees2) remain2 \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline -{}- Building a parse tree from list of found parse trees \newline parse :: TreeBuilder -\textgreater{} Parser -\textgreater{} Parser \newline parse builder parser input = \newline case parser input of \newline FAIL -\textgreater{} FAIL \newline (OK {[}{]} remain) -\textgreater{} OK {[}{]} remain \newline (OK trees remain) -\textgreater{} OK {[}builder trees{]} remain} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}