% mrh 17/1 2013
% $Header: /cvsroot/latex-beamer/latex-beamer/solutions/generic-talks/generic-ornate-15min-45min.en.tex,v 1.5 2007/01/28 20:48:23 tantau Exp $
\documentclass[9pt,xcolor=dvipsnames]{beamer}
%\documentclass[handout,9pt,xcolor=dvipsnames]{beamer}



\mode<presentation>
{
  \usetheme{DTU}
  % or ...


  \setbeamercovered{transparent}

  % or whatever (possibly just delete it)
}


\usepackage[english]{babel}
% or whatever

\usepackage[latin1]{inputenc}
% or whatever

\usepackage{palatino}
%\usepackage[T1]{fontenc}
\usepackage{mrhalltt}
\usepackage{itemlist}
%\usepackage{fix-cm}

\input{macros}


%\usepackage{pgfpages}
%\pgfpagesuselayout{4 on 1}[letterpaper,landscape,bordershrink=5mm]

\title % (optional, use only with long paper titles)
{02157 Functional Programming}

\subtitle
{Tagged values and Higher-order list functions} % (optional)

\author% (optional, use only with lots of authors)
{Michael~R.~Hansen}
% - Use the \inst{?} command only if the authors have different
%   affiliation.

\institute % (optional, but mostly needed)
{DTU Informatics, Technical University of Denmark}

\date {MRH 27/09/2012}% (optional)
%{Advanced Topics on Embedded Systems}

%\subject{Talks}
% This is only inserted into the PDF information catalog. Can be left
% out.



% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

 \pgfdeclareimage[height=.75cm]{university-logo}{DTUlogo}
 \logo{\pgfuseimage{university-logo}}

 \pgfdeclareimage[height=3.3cm]{frise}{DTUfrise}
 \pgfdeclareimage[height=.8cm]{dept}{DTUInformatics}


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:

%\beamerdefaultoverlayspecification{<+->}



\begin{document}

\begin{frame}
  \titlepage
%  \begin{tabular}{p{5cm}p{10cm}}
%    &\pgfuseimage{frise}\\
%    \pgfuseimage{dept} &
%  \end{tabular}

\end{frame}

%\begin{frame}{Outline}
%  \tableofcontents
  % You might wish to add the option [pausesections]
%\end{frame}



\begin{frame}[fragile]\frametitle{Part I: Disjoint Sets -- An Example}

A {\em\blue shape} is either a {\redbrown circle}, a {\redbrown
square}, or a {\redbrown triangle}

\begin{itemize}
\item %\fromSlide{2}{
the union of {\redbrown three disjoint} sets%}
\end{itemize}


\medskip
\pause

\begin{verbatim}
type shape =
    Circle of float
  | Square of float
  | Triangle of float*float*float;;
\end{verbatim}

\bigskip
\pause

The {\em\blue tags} \texttt{\normalsize\redbrown Circle},
\texttt{\normalsize\redbrown Square} and
\texttt{\normalsize\redbrown Triangle} are {\em\blue
constructors}:

\begin{verbatim}
- Circle 2.0;;
> val it : shape = Circle 2.0

- Triangle(1.0, 2.0, 3.0);;
> val it : shape = Triangle(1.0, 2.0, 3.0)

- Square 4.0;;
> val it : shape = Square 4.0
\end{verbatim}

%\bigskip
%\pause

%Equality on \texttt{\normalsize\redbrown shape}s is defined
%because ${\redbrown \ldots}$

%\bigskip

%\begin{tabular}{l}
%\texttt{- Triangle(1.0, 2.0, 3.0) {\redbrown =} Square 2.0;}\\
%\texttt{> val it = false : bool}
%\end{tabular}
\end{frame}


\begin{frame}[fragile]\frametitle{Constructors in Patterns}

A shape-area function is declared

{\blue
\begin{verbatim}
let area = function
  | Circle r        -> System.Math.PI * r * r
  | Square a        -> a * a
  | Triangle(a,b,c) ->
      let s = (a + b + c)/2.0
      sqrt(s*(s-a)*(s-b)*(s-c)) ;;
> val area : shape -> real
\end{verbatim}}
following the structure of shapes.


\pause
\bigskip
\begin{itemize}
\item a constructor only matches itself
\end{itemize}

\pause

\[\blue
\begin{array}{ll}
     & \verb?area (Circle 1.2)?\\
\lto & (\verb/System.Math.PI * r * r/,\ \  [\mathtt{r} \mapsto \verb-1.2-]) \\
\lto & \ldots
\end{array}
\]
\end{frame}



\begin{frame}[fragile]\frametitle{Enumeration types -- the months}

Months are naturally defined using tagged values::
\begin{alltt}\blue
type month = January | February | March | April
             | May | June | July | August | September
             | October | November | December ;;
\end{alltt}

\pause
\medskip

The days-in-a-month function is declared by
\begin{alltt}\blue
let daysOfMonth = function
  | February                            -> 28
  | April | June | September | November -> 30
  | _                                   -> 31 ;;
\ett{val daysOfMonth : month -> int}
\end{alltt}



\end{frame}


\begin{frame}[fragile]\frametitle{The \texttt{\Large\redbrown option} type}

{\blue
\begin{alltt}
type 'a option = None | Some of 'a
\end{alltt}
}

Distinguishes the cases "nothing" and "something".\\
\hfill {\redbrown predefined}


\bigskip
\pause

The constructor \verb/Some/ and \verb-None- are polymorphic:
\begin{alltt}\blue
Some false;;
\ett{val it : bool option = Some false}

Some (1, "a");;
\ett{val it : (int * string) option = Some (1, "a")}

None;;
\ett{val it : 'a option = None}
\end{alltt}

\end{frame}

\begin{frame}[fragile]\frametitle{Example}

Find first position of element in a list:

\begin{alltt}\blue
let rec findPosI p x = function
   | y::_ when x=y -> Some p
   | _::ys         -> findPosI (p+1) x ys
   | []            -> None;;
\ett{val findPosI : int -> 'a -> 'a list -> int option when ...}
\pause

let findPos x ys = findPosI 0 x ys;;
\ett{val findPos : 'a -> 'a list -> int option when ...}
\end{alltt}

\pause
\medskip
Examples
\begin{alltt}\blue
findPos 4 [2 .. 6];;
\ett{val it : int option = Some 2}

findPos 7 [2 .. 6];;
\ett{val it : int option = None}

{\redbrown Option.get}(findPos 4 [2 .. 6]);;
\ett{val it : int = 2}
\end{alltt}

\end{frame}



\begin{frame}{Part 2:Motivation}

Higher-order functions are

\begin{itemize}

\item everywhere\\[3mm]
\qquad $\blue \Sigma_{i=a}^b f(i),\ \frac{d f}{d x},\ \{x \in A\ |
\ P(x) \}, \ldots $

\item<2-> powerful\\[3mm]
\qquad Parameterized modules succinct code
$\blue \ldots$

\end{itemize}

\pause[3]
\bigskip
\begin{center}\large\redbrown HIGHER-ORDER FUNCTIONS ARE USEFUL \end{center}

\end{frame}


\begin{frame}[fragile]


\ \hfill{\blue now down to earth}

\pause

\begin{itemize}
\item Many recursive declarations follows the same schema.
\end{itemize}

For example:

{\blue
\begin{verbatim}
let rec f = function
  | []    ->   ...
  | x::xs ->   ... f(xs) ...
\end{verbatim}
}
\bigskip
\pause

\begin{center}\redbrown
Succinct declarations achievable using higher-order functions
\end{center}

Contents

\begin{itemize}

\item Higher-order list functions (in the library)
\begin{itemize}
\item map
\item exists, forall, filter, tryFind
\item foldBack, fold
\end{itemize}

\end{itemize}

\pause
\bigskip

\begin{center}\blue
Avoid (almost) identical code fragments by\\
\redbrown parameterizing functions with functions
\end{center}


\end{frame}



\begin{frame}[fragile]\frametitle{A simple declaration of a list function}

A typical declaration following the structure of lists:
\begin{alltt}
let rec posList = function
    | []    -> []
    | x::xs -> (x > 0)::posList xs;;
\ett{val posList : int list -> bool list}

posList [4;  -5; 6];;
\ett{val it : bool list = [true; false; true]}
\end{alltt}

\bigskip
\pause

{\redbrown Applies the function  {\blue \texttt{fun\
x\ ->\ x\
>\ 0}} to each element in a list}

\end{frame}


\begin{frame}[fragile]\frametitle{Another declaration with the same structure}

\begin{alltt}
let rec addElems = function
   | []        ->  []
   | (x,y)::zs -> (x+y)::addElems zs;;
\ett{val addElems : (int * int) list -> int list}

addElems [(1,2) ;(3,4)];;
\ett{val it : int list = [3; 7]}
\end{alltt}

\bigskip
\pause
{\redbrown Applies the addition function} {\blue
\texttt{+}} {\redbrown to each pair of integers in a list}

\end{frame}


\begin{frame}[fragile]\frametitle{The function:\ \ {\Large\tt\redbrown map}}

{\redbrown Applies a function to each element in a list} {\blue \[
\mathtt{map}\ f\ [v_1; v_2; \ldots; v_n ]\ =\ [f(v_1); f(v_2);
\ldots; f(v_n)]
\]}

{\redbrown Declaration} \hfill {\darkgreen Library function}
\begin{alltt}
let rec map f = function
   | []    -> []
   | x::xs -> f x :: map f xs;;
\ett{val map : ('a -> 'b) -> 'a list -> 'b list}
\end{alltt}

\bigskip
\pause

{\redbrown Succinct declarations can be achieved using map, e.g.}
\begin{alltt}
let posList = map (fun x -> x > 0);;
\ett{val posList : int list -> bool list}

let addElems = map (fun (x,y) -> x+y);;
\ett{val addElems : (int * int) list -> int list}
\end{alltt}

\end{frame}


\begin{frame}{Exercise}

Declare a function {\blue \[ \mathtt{g}\ [x_1, \ldots, x_n] =
[x_1^2+1, \ldots, x_n^2+1]
\]}

\bigskip

\bigskip

Remember {\blue \[ \mathtt{map}\ f\ [v_1; v_2; \ldots; v_n ]\ =\
[f(v_1); f(v_2); \ldots; f(v_n)]
\]}


\end{frame}


\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown exists}}


{\redbrown Predicate: \blue For some $x$ in $xs: p(x)$.}

\pause

{\blue
\[
\mathtt{exists}\;p\;\mathit{xs} = \left\{
\begin{array}{ll}
\mathrm{true} & \mbox{if $p(x) =  \mathtt{true}$ for some $x$ in $\mathit{xs}$}\\
\mathrm{false} & \mbox{otherwise}
\end{array}
\right.
\]
}

\pause

{\redbrown Declaration}\hfill {\darkgreen Library function}
\begin{alltt}
let rec exists p = function
  | []    -> false
  | x::xs -> p x || exists p xs;;
\ett{val exists : ('a -> bool) -> 'a list -> bool}
\end{alltt}

\bigskip
\pause

{\redbrown Example}
\begin{alltt}
exists (fun x -> x>=2) [1; 3; 1; 4];;
\ett{val it : bool = true}
\end{alltt}

\end{frame}

\begin{frame}{Exercise}

Declare \texttt{\blue isMember} function using \texttt{\blue
exists}.

\bigskip

\begin{alltt}
let isMember x ys = exists  {\redbrown ?????} ;;\\
\ett{val isMember : 'a -> 'a list -> bool  when 'a : equality}
\end{alltt}

\bigskip

\bigskip

Remember {\blue
\[
\mathtt{exists}\;p\;\mathit{xs} = \left\{
\begin{array}{ll}
\mathrm{true} & \mbox{if $p(x) =  \mathtt{true}$ for some $x$ in $\mathit{xs}$}\\
\mathrm{false} & \mbox{otherwise}
\end{array}
\right.
\]
}

\end{frame}



\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown forall}}


{\redbrown Predicate: \blue For every $x$ in $xs: p(x)$.}

\pause

{\blue
\[
\mathtt{forall}\;p\;\mathit{xs} = \left\{
\begin{array}{ll}
\mathrm{true} & \mbox{if $p(x) =  \mathrm{true}$, for all elements
$x$ in
\textit{xs}}\\
\mathrm{false} & \mbox{otherwise}
\end{array}
\right.
\]}

\bigskip
\pause

{\redbrown Declaration}\hfill {\darkgreen Library function}
\begin{alltt}
let rec forall p = function
  | []    -> true
  | x::xs -> p x && forall p xs;;
\ett{val forall : ('a -> bool) -> 'a list -> bool}
\end{alltt}

\bigskip
\pause

{\redbrown Example}
\begin{alltt}
forall (fun x -> x>=2) [1; 3; 1; 4];;
\ett{val it : bool = false}
\end{alltt}

\end{frame}

\begin{frame}{Exercises}

\onslide<1>{
Declare a function
\[{\blue
\mathtt{disjoint}\ \mathit{xs}\ \mathit{ys}}
\]
which is true when there are no common elements in the lists
{\blue $\mathit{xs}$} and {\blue $\mathit{ys}$}, and false
otherwise.
}

\bigskip

\onslide<2>{
Declare a function
\[{\blue
\mathtt{subset}\ \mathit{xs}\ \mathit{ys}}
\]
which is true when every element in the lists {\blue
$\mathit{xs}$} is in  {\blue $\mathit{ys}$}, and false otherwise.
}
\bigskip

\bigskip

Remember {\blue
\[
\mathtt{forall}\;p\;\mathit{xs} = \left\{
\begin{array}{ll}
\mathrm{true} & \mbox{if $p(x) =  \mathrm{true}$, for all elements
$x$ in
\textit{xs}}\\
\mathrm{false} & \mbox{otherwise}
\end{array}
\right.
\]}


\end{frame}



\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown filter}}


{\redbrown Set comprehension: \blue ${\{ x \in xs: p(x) \}}$}

\pause
\bigskip

{\blue $\mathtt{filter}\;p\;\mathit{xs}$} is the list of those
elements {\blue $x$} of {\blue \textit{xs}} where {\redbrown $p(x)
= \mathtt{true}$}.

\bigskip
\pause

{\redbrown Declaration}\hfill {\darkgreen Library function}
\begin{alltt}
let rec filter p = function
  | []     -> []
  | x::xs -> if p x then x :: filter p xs
             else filter p xs;;
\ett{val filter : ('a -> bool) -> 'a list -> 'a list}
\end{alltt}

\bigskip
\pause

{\redbrown Example}
\begin{alltt}
filter System.Char.IsLetter ['1'; 'p'; 'F'; '-'];;
\ett{val it : char list = ['p'; 'F']}
\end{alltt}

\bigskip

where
 \verb-System.Char.IsLetter- $c$ is true
iff $c \in \{\mathtt{'A'}, \ldots, \mathtt{'Z'} \} \cup \{
\mathtt{'a'}, \ldots, \mathtt{'z'} \}$


\end{frame}


\begin{frame}{Exercise}

Declare a function
\[
{\blue \mathtt{inter}\ \mathit{xs}\ \mathit{ys}}
\]
which contains the common elements of the lists {\blue
$\mathit{xs}$} and {\blue $\mathit{ys}$}  --- i.e.\ their
intersection.


\bigskip

\bigskip

Remember:\\
{\blue $\mathtt{filter}\;p\;\mathit{xs}$} is the list of
those elements {\blue $x$} of {\blue \textit{xs}} where {\redbrown
$p(x) = \mathtt{true}$}.


\end{frame}

\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown tryFind}}

\[
{\blue \mathtt{tryFind}\;p\;\mathit{xs}} = \left\{ \begin{array}{lcl}
{\blue \mathtt{Some}\;x} & & \text{for an element $\blue x$ of \textit{\blue xs} with $\blue p(x) =
  \mathrm{true}$}\\
  {\blue \mathtt{None}} & & \text{if no such element exists}
  \end{array} \right.
\]

\bigskip
\pause

\begin{alltt}
let rec tryFind p = function
  | x::xs when p x -> Some x
  | _::xs          -> tryFind p xs
  | _              -> None ;;
\ett{val tryFind : ('a -> bool) -> 'a list -> 'a option}
\pause

tryFind (fun x -> x>3) [1;5;-2;8];;
\ett{val it : int option = Some 5}
\end{alltt}
\end{frame}


\begin{frame}[fragile]\frametitle{Folding a function over a list (I)}

Example: sum of norms of geometric vectors:

\begin{alltt}
let norm(x1:float,y1:float) = sqrt(x1*x1+y1*y1);;
\ett{val norm : float * float -> float}

{\blue
let rec sumOfNorms = function
    | []    -> 0.0
    | v::vs -> norm v + sumOfNorms vs;;
\ett{val sumOfNorms : (float * float) list -> float}
}

let vs = [(1.0,2.0); (2.0,1.0); (2.0, 5.5)];;
\ett{val vs : (float * float) list}
\ett{       = [(1.0, 2.0); (2.0, 1.0); (2.0, 5.5)]}

sumOfNorms vs;;
\ett{val it : float = 10.32448591}
\end{alltt}

\end{frame}

\begin{frame}[fragile]\frametitle{Folding a function over a list (II)}

\begin{alltt}
\blue
let rec sumOfNorms = function
    | []    -> 0.0
    | v::vs -> norm v + sumOfNorms vs;;
\end{alltt}

Let $\blue f\ v\ s$ abbreviate $\blue \mathtt{norm}\ v + s$ in the evaluation:

\[\begin{array}{cl}
       & \mathtt{sumOfNorms}\ [v_0; v_1; \ldots; v_{n-1}]\\
\lto   & \mathtt{norm}\ v_0 + (\mathtt{sumOfNorms}\ [v_1; \ldots; v_{n-1}]) \\
=      & f\ v_0\ (\mathtt{sumOfNorms}\ [v_1; \ldots; v_{n-1}])\\
\lto   & f\ v_0\ (f\ v_1\ (\mathtt{sumOfNorms} [v_2; \ldots; v_{n-1}]))\\
\vdots & \\
\lto   & {\darkgreen f}\ v_0\ ({\darkgreen f}\ v_1\ ( \cdots ({\darkgreen f}\ v_{n-1}\ 0.0) \cdots ))
\end{array}
\]

\pause

This repeated application of $\darkgreen f$ is also called a {\redbrown folding} of $\darkgreen f$.

\pause
\medskip

\begin{center}\redbrown
Many functions follow such recursion and evaluation schemes
\end{center}

\end{frame}


\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown foldBack} (1)}

Suppose that  $\blue \otimes$ is an infix function. Then

\bigskip

{\blue
\begin{tabular}{lcl}
\multicolumn{3}{l}
 {\tt foldBack ($\otimes$) [$a_0$; $a_1$; $\ldots$ ; $a_{n-2}$; $a_{n-1}$] $e_b$}
\\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ & = & $  a_{0} \otimes ( a_{1} \otimes (  \ldots ( a_{n-2} \otimes ( a_{n-1} \otimes e_b )) \ldots ))$
\end{tabular}
}

\bigskip
\pause

\[\blue
\begin{array}{rclcc}
\verb!List.foldBack (+) [1; 2; 3] 0! & = & 1 + (2+(3+0)  & = & 6\\
\verb!List.foldBack (-) [1; 2; 3] 0! & = & 1 - (2-(3-0)) & = & 2
\end{array}
\]

\medskip
\pause

Using the cons operator gives the append function \verb/@/ on lists:
\medskip\pause

{\blue
\begin{tabular}{l}
{\tt foldBack (fun x rst -> x::rst) [$x_0$; $x_1$; $\ldots$; $x_{n-1}$] ys} \\
\ \ \ \ \ = {\tt $x_0$::($x_1$:: $\ldots$ ::($x_{n-1}$::ys) $\ldots$ ))} \\
\ \ \ \ \ = {\tt [$x_0$; $x_1$; $\ldots$; $x_{n-1}$] @ ys}
\end{tabular}}

\medskip\pause
so we get:
\begin{alltt}\blue
let (@) xs ys = List.foldBack (fun x rst -> x::rst) xs ys;;
\ett{val ( @ ) : 'a list -> 'a list -> 'a list}

[1;2] @ [3;4] ;;
\ett{val it : int list = [1; 2; 3; 4]}
\end{alltt}

\end{frame}



\begin{frame}[fragile]\frametitle{Declaration of {\Large\tt\redbrown foldBack}}

\begin{alltt}\blue
let rec foldBack f xlst e =
  match xlst with
  | x::xs -> f x (foldBack f xs e)
  | []    -> e  ;;
\ett{val foldBack : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b}
\end{alltt}

\pause
\bigskip

\begin{verbatim}
let sumOfNorms vs = foldBack (fun v s -> norm v + s)  vs 0.0;;

let length xs = foldBack (fun _ n -> n+1) xs 0;;

let map f xs = foldBack (fun x rs -> f x :: rs) xs [];;

\end{verbatim}

\end{frame}


\begin{frame}[fragile]\frametitle{Exercise: union of sets}

Let an insertion function be declared by

{\blue
\begin{verbatim}
let insert x ys = if isMember x ys then ys else x::ys;;
\end{verbatim}
}

\bigskip

Declare a union function on sets, where a set is represented by a
list without duplicated elements.

\bigskip

\bigskip

Remember:
\[
{\blue \verb?foldBack?\ (\oplus)\,
\mathtt{[}x_1\mathtt{;} x_2\mathtt{;} \ldots\mathtt{;}
x_n\mathtt{]}\ b\ \lto\
 x_1 \oplus (x_2 \oplus \cdots \oplus (x_n \oplus
       b) \cdots )}
\]

\end{frame}


\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown fold} (1)}

Suppose that $\oplus$ is an infix function.

\medskip

Then the {\redbrown\tt fold} function has the definitions:

\medskip

{\blue
\begin{tabular}{lcl}
\multicolumn{3}{l}
 {\tt fold ($\oplus$) $e_a$ [$b_0$; $b_1$; $\ldots$ ; $b_{n-2}$; $b_{n-1}$ ]}
\\
 & = & $ (( \ldots (( e_a \oplus b_0) \oplus b_1 ) \ldots ) \oplus b_{n-2} ) \oplus b_{n-1} $
\end{tabular}
}

\medskip

i.e. it applies $\blue \oplus$ from left to right.

\bigskip
\pause
Examples:
\[\blue
\begin{array}{rcccc}
\verb!List.fold (-) 0 [1; 2; 3]!     & = & ((0-1) -2)-3 & = & -6\\
\pause
\verb!List.foldBack (-) [1; 2; 3] 0! & = & 1 -(2-(3-0)) & = & 2
\end{array}
\]




\end{frame}

\begin{frame}[fragile]\frametitle{Higher-order list functions: {\Large\tt\redbrown fold} (2)}

\begin{alltt}\blue
let rec fold f e = function
  | x::xs -> fold f (f e x) xs
  | []    -> e  ;;
\ett{val fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a}
\end{alltt}

\bigskip
\pause

Using cons in connection with {\blue fold} gives the reverse function:

\begin{alltt}
let rev xs  = fold (fun rs x -> x::rs) [] xs;;
\end{alltt}

This function has a linear execution time:
\pause
\medskip

{\blue
\begin{tabular}{lllll}
       &  \multicolumn{3}{l}{\tt rev [1;2;3]}    \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & {\redbrown []} & {\tt [1;2;3]} \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & ({\redbrown 1::[]}) & {\tt [2;3]} \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & {\redbrown [1]}  &{\tt [2;3]} \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & ({\redbrown 2::[1]}) & {\tt [3]} \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & {\redbrown [2\,;\,1]} & {\tt [3]} \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & ({\redbrown 3::[2\,;\,1]}) & {\tt []} \\
$\lto$ & {\tt fold} & {\tt (fun ...)} & {\redbrown [3\,;\,2\,;\,1]} & {\tt []} \\
$\lto$ & [3;2;1]
\end{tabular}
}


\end{frame}

\begin{frame}[fragile]\frametitle{Summary}


\begin{itemize}
\item Many recursive declarations follows the same schema.
\end{itemize}

For example:

{\blue
\begin{verbatim}
fun f []     =  ...
  | f(x::xs) =  ... f(xs) ...
\end{verbatim}
}
\bigskip

\begin{center}\redbrown
Succinct declarations achievable using higher-order functions
\end{center}

Contents

\begin{itemize}

\item Higher-order list functions (in the library)
\begin{itemize}
\item map \item exists, forall, filter, tryFind  \item foldBack, fold
\end{itemize}

\end{itemize}

\pause
\bigskip

\begin{center}\blue
Avoid (almost) identical code fragments by\\
\redbrown parameterizing functions with functions
\end{center}


\end{frame}

\end{document}
