\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{kvp\_} \pdfinfo{ /Title (comp302.pdf) /Creator (Cheatography) /Author (kvp\_) /Subject (COMP302 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{COMP302 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{kvp\_} via \textcolor{DarkBackground}{\uline{cheatography.com/20610/cs/3336/}}} \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}kvp\_ \\ \uline{cheatography.com/kvp} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 20th February, 2015.\\ Updated 13th May, 2016.\\ 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*}{2} \begin{tabularx}{8.4cm}{x{5.52 cm} x{2.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{SML Syntax}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{String}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `\#"str"` & character \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `String.sub : string * int -\textgreater{} char` & n-th character \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} `chr` & ascii to character \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} `ord : char -\textgreater{} int` & character to ascii \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} `\textasciicircum{}` & concatenate \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} `String.tokens : (char -\textgreater{} bool) -\textgreater{} string -\textgreater{} string list` & tokenize a string \tn % Row Count 12 (+ 3) % Row 7 \SetRowColor{white} `String.explode : string -\textgreater{} char list` & also implode \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{List}}} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} `@ : 'a list @ 'a list` & \seqsplit{concatenation} \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} `List.partition : ('a -\textgreater{} bool) -\textgreater{} 'a list -\textgreater{} 'a list * 'a list` & quicksort \tn % Row Count 20 (+ 3) % Row 11 \SetRowColor{white} `List.rev : 'a list -\textgreater{} 'a list` & reverse \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} `List.exists : ('a -\textgreater{} bool) -\textgreater{} 'a list -\textgreater{} bool` & true for any \tn % Row Count 24 (+ 2) % Row 13 \SetRowColor{white} `List.all : ('a -\textgreater{} bool) -\textgreater{} 'a list -\textgreater{} bool` & true for all \tn % Row Count 26 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`String.concatWith : string -\textgreater{} string list -\textgreater{} string`} \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Referential Transparency}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Replace any expression with another expression of "equal" value does not affect the value of the expression% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Equivalence}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Two programs are equivalent iff \newline % Row Count 1 (+ 1) 1. They both evaluate to the same value, or \newline % Row Count 2 (+ 1) 2. They both raise the same exception, or \newline % Row Count 3 (+ 1) 3. They both enter an infinite loop \newline % Row Count 4 (+ 1) Properties \newline % Row Count 5 (+ 1) 1. Equivalence is an equivalence relation \newline % Row Count 6 (+ 1) 2. Equivalence is a congruence (one program can be substituted for another) \newline % Row Count 8 (+ 2) 3. If e |-\textgreater{} e' then e is equivalent to e'% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Valuable \& Total}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Expression {\bf{e}} is valuable iff there is some value {\bf{v}} s.t. {\bf{e}} == {\bf{v}} \newline % Row Count 2 (+ 2) - If {\bf{e}} = ({\bf{e1}},{\bf{e2}}) \newline % Row Count 3 (+ 1) - If {\bf{e}} = {\bf{e1}}+{\bf{e2}} \newline % Row Count 4 (+ 1) - If {\bf{e}} = {\bf{e1}} :: {\bf{e2}} \newline % Row Count 5 (+ 1) then e is valuable iff e1 is valuable and e2 is valuable \newline % Row Count 7 (+ 2) A function {\bf{f}} : A -\textgreater{} B is total iff for all values {\bf{v}} : A,{\bf{ f(v)}} is valuable% Row Count 9 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Currying}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{=== Non-curried === \newline fun pow (x, y) : int {\emph{ int -\textgreater{} int = \newline case y of` \newline 0 =\textgreater{} 1` \newline | ~ \_ =\textgreater{} x }} pow(x, y-1) \newline === Curried === \newline fun pow x : int -\textgreater{} int -\textgreater{} int = \newline fn (y) =\textgreater{} case y of \newline 0 =\textgreater{} 1 \newline | ~ \_ =\textgreater{} x {\emph{ pow(x, y-1) \newline fun pow x y = \newline case y of \newline 0 =\textgreater{} 1 \newline | ~ \_ =\textgreater{} x }} pow(x, y-1) \newline === Currying and Uncurrying === \newline curry : (('a * 'b) -\textgreater{} 'c) -\textgreater{} ('a -\textgreater{} 'b -\textgreater{} 'c)) \newline fun curry f x y = f (x,y) \newline fun uncurry f (x,y) = f x y \newline uncurry : ('a -\textgreater{} 'b -\textgreater{} 'c) -\textgreater{} (('a 'b) -\textgreater{} 'c)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Composition}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`fun compose (f, g) = fn x =\textgreater{} f(g x)` \newline % Row Count 1 (+ 1) Using infix operator \newline % Row Count 2 (+ 1) `val sqrt\_of\_abs = Math.sqrt o Real.fromInt o abs` \newline % Row Count 4 (+ 2) Pipelining and infix pipeline operator \newline % Row Count 5 (+ 1) `fun pipeline (f, g) = g f \newline % Row Count 6 (+ 1) `infix !\textgreater{}` \newline % Row Count 7 (+ 1) `fun x !\textgreater{} f = f x` \newline % Row Count 8 (+ 1) `fun sqrt\_of\_abs i =i !\textgreater{} Real.fromInt !\textgreater{} Math.sqrt` \newline % Row Count 10 (+ 2) `datatype 'a list = Nil | :: of 'a * 'a list`% Row Count 12 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Mergesort}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{fun split (lst : int list) : int list * int list = \newline case lst of \newline {[}{]} =\textgreater{} ({[}{]}, {[}{]}) \newline | ~ {[}x{]} =\textgreater{} ({[}x{]}, {[}{]}) \newline | ~ x::y::xs =\textgreater{} let val (pile1, pile2) = split xs \newline in (x::pile1, y::pile2) \newline end \newline fun merge(lst1 : int list, lst2 : int list) : int list = \newline case (lst1, lst2) of \newline ({[}{]}, lst2) =\textgreater{} lst2 \newline | ~ (lst1, {[}{]}) =\textgreater{} lst1 \newline | ~ (x::xs, y::ys) =\textgreater{} \newline (case x \textless{} y of \newline true =\textgreater{} x::merge(xs,lst2) \newline | ~ false =\textgreater{} y::merge(lst1, ys) \newline fun mergesort (lst : int list) : int list = \newline case lst of \newline {[}{]} =\textgreater{} {[}{]} \newline | ~ {[}x{]} =\textgreater{} {[}x{]} \newline | \_ =\textgreater{} let val (pile1, pile2) = split lst \newline in merge(mergesort pile1, mergesort pile2) \newline end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Generalized math functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{fun sum (f, a, b, inc) : \newline if (a \textgreater{} b) then 0 \newline else (f a) + sum(f, inc(a), b, inc) \newline \newline fun piOver8 = sum(fn x =\textgreater{} 1.0 / (x{\emph{(x+2.0)), a, b, fn x =\textgreater{} x + 4.0) \newline \newline fun integral (f, a, b, dx) = \newline dx }} sum(f, a+dx/2.0, b, fn x =\textgreater{} x+dx) \newline \newline fun series (operator, f, lo, hi, inc, identity) = \newline if (lo \textgreater{} hi) then identity \newline else operator((f lo), series (operator, f, inc(lo), hi, inc, identity)) \newline \newline fun sumSeries (f, a, b, inc) = series (op +, f, a, b, inc, 0) \newline fun prodSeries(f, a, b, inc) = series(op *, f, a, b, inc, 1)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data types}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{User defined types \newline % Row Count 1 (+ 1) `datatype tree = Empty | Node of tree * int * tree` \newline % Row Count 3 (+ 2) `datatype 'a option = NONE | SOME of 'a` \newline % Row Count 4 (+ 1) Type synonym \newline % Row Count 5 (+ 1) `type intPairList = (int * int) list`% Row Count 6 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Map}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{map : ('a -\textgreater{} 'b) * 'a list -\textgreater{} b' list \newline fun map (f lst) = \newline case lst of \newline {[}{]} =\textgreater{} {[}{]} \newline | ~ h::tail =\textgreater{} (f h)::(map f tail)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Fold}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{fun foldl(f, acc, lst) = \newline case lst of \newline {[}{]} =\textgreater{} acc \newline | ~ h::t =\textgreater{} foldl(f, f(h, acc), t) (*tail recursive*) \newline fun foldr(f, acc, lst) = \newline case lst of \newline {[}{]} =\textgreater{} acc \newline | ~ h::t =\textgreater{} f(h, foldr(f, acc, t)) (*not tail recursive*)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Associativity}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`'a -\textgreater{} 'b -\textgreater{} 'c = 'a -\textgreater{} ('b -\textgreater{} 'c)` \newline % Row Count 1 (+ 1) `f a1 a2 = (f a1) a2`% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Filter}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{filter : ('a -\textgreater{} bool) * 'a list -\textgreater{} 'a list \newline fun filter (p : 'a -\textgreater{} bool, lst : 'a list) = \newline case lst of \newline {[}{]} =\textgreater{} {[}{]} \newline | ~ x::xs =\textgreater{} if p x then x::(filter p xs) \newline else filter p xs} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}