From c135405b1cd1ce9f9495b56a99bd8c302a7b6821 Mon Sep 17 00:00:00 2001 From: pimjager Date: Mon, 30 May 2016 17:31:15 +0200 Subject: [PATCH] Working on slides for p4 --- deliverables/p4/p4.tex | 62 +++++++++++++++++++++++++++++++++++++++++ deliverables/p4/spl.sty | 30 ++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 deliverables/p4/spl.sty diff --git a/deliverables/p4/p4.tex b/deliverables/p4/p4.tex index 382f8ee..adc62d2 100644 --- a/deliverables/p4/p4.tex +++ b/deliverables/p4/p4.tex @@ -3,6 +3,7 @@ \usepackage{xcolor} \usepackage{listings} \usepackage{clean} +\usepackage{spl} \title[cc1516]{SPLC} \subtitle{\texttt{~::= `,' `and' }} @@ -25,4 +26,65 @@ \begin{document} \frame{\titlepage} + +\begin{frame}[fragile] + \frametitle{Higher order functions implemented using function pointers} + + \begin{SPLCode} +map(f, xs) :: (a -> b) -> [a] -> [b] { + if( isEmpty(xs) ) { return []; } + else { return f(xs.hd) : map(f, xs.tl); } +} +id(x) { return x; } + +main() { + map(id, 1:2:3:[]); +} + \end{SPLCode} + \pause + \begin{itemize} + \item Function arguments are passed by name. + \item During compilation these are replaced by unique function ID's + \item During runtime the corresponding function label is retrieved using + a dictionary like approach. + \end{itemize} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Functions application can be curried} + + \begin{SPLCode} +plus(x,y) :: Int -> Int -> Int { + return x + y; +} + +main() { + map(plus(1), 1:2:3:[]); +} + \end{SPLCode} + \pause + \begin{columns}[T] + \begin{column}[T]{0.5\textwidth} + \begin{enumerate} + \item Function ID is placed on the heap + \item Number of passed arguments is placed on the heap + \item Arguments are pushed on the heap + \end{enumerate} + \end{column} + \pause + \begin{column}[T]{0.5\textwidth} + \begin{block}{Heap contents} + \begin{tabular}{c | l | r} + address & Content & \\ \hline + n & 8 & (1)\\ + n+1 & 1 & (2)\\ + n+2 & 4 & (3) + \end{tabular} + \end{block} + \end{column} + \end{columns} + +\end{frame} + \end{document} diff --git a/deliverables/p4/spl.sty b/deliverables/p4/spl.sty new file mode 100644 index 0000000..8533252 --- /dev/null +++ b/deliverables/p4/spl.sty @@ -0,0 +1,30 @@ +\usepackage{listings} + +\lstdefinelanguage{SPLCode}{% + alsoletter={ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_1234567890}, + morekeywords={print,isEmpty,if,else,while,read}, + sensitive=true, + morecomment=[l]{//}, + morecomment=[n]{/*}{*/}, + morestring=[b]", + morestring=[b]', + basicstyle=\small, + identifierstyle=\small\ttfamily, + commentstyle=\itshape, + keywordstyle=\bfseries, + stringstyle=\ttfamily, + showstringspaces=false, + basewidth=0.45em, + columns=[c]fixed, + keepspaces=true, + breaklines=true, + tabsize=4, + texcl=true, +} + +\newcommand{\SPLInline}[1]{\lstinline[language=SPLCode]¦#1¦} +\newcommand{\SI}[1]{\SPLInline{#1}} + +\lstdefinestyle{numbers}{numbers=left, stepnumber=1, numberstyle=\tiny, numbersep=5pt} + +\lstnewenvironment{SPLCode}{\lstset{language=SPLCode}}{} -- 2.20.1