\documentclass{book}
\renewcommand{\chaptername}{Lecture}
\usepackage{amsmath}
\usepackage{ctable}
\usepackage[mathletters]{ucs}
\usepackage[utf8x]{inputenc}
% Redefine labelwidth for lists; otherwise, the enumerate package will cause
% markers to extend beyond the left margin.
\makeatletter\AtBeginDocument{%
  \renewcommand{\@listi}
    {\setlength{\labelwidth}{4em}}
}\makeatother
\usepackage{enumerate}
\usepackage[breaklinks=true,unicode=true]{hyperref}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
%\setcounter{secnumdepth}{0}


\begin{document}

\setcounter{chapter}{2}

\chapter{More on pattern matching.}

In the last lecture we, defined the factorial function and illustrated
the use of pattern matching. In this chapter we elaborate on it a bit
more.

\section{Pattern matching on lists.}

We have already seen the list type. There is an algebraic way of
defining a list. A list either an empty list or an element attached to
the front of an already constructed list. An empty list in Haskell is
expressed as \texttt{{[}{]}} where as a list whose first element is
\texttt{x} and the rest is \texttt{xs} is denoted by \texttt{x:xs}. The
notation \texttt{{[}1,2,3{]}} is just a
\emph{\href{http://en.wikipedia.org/wiki/Syntactic\_sugar}{syntactic
sugar}} for \texttt{1:(2:(3:{[}{]}))} or just \texttt{1:2:3:{[}{]}} as
\texttt{:} is a right associative operator. We now illustrate pattern
matching on list by giving the example of the \texttt{map} function. The
\texttt{map} function takes a function and a list and applies the
function on each element of the list. Here is the complete definition
including the type

\begin{verbatim}

> import Prelude hiding (map, null, curry, uncurry)
>        -- hide Prelude functions defined here
>
> map :: (a -> b) -> [a] -> [b]
> map f []           = []
> map f (x:xs)       = f x : map f xs
\end{verbatim}
Since a list can either be empty or of the form \texttt{(x:xs)} this is
a complete definition. Also notice that pattern matching of variables is
done here.

\section{Literate programming detour.}

Before we proceed further, let us clarify the \texttt{\textgreater{}}'s
at the beginning of the of the lines. This is the
\href{http://en.wikipedia.org/wiki/Literate\_programming}{literate
programming} convention that Haskell supports.
\href{http://en.wikipedia.org/wiki/Literate\_programming}{Literate
programming} is a style of programming championed by
\href{http://www-cs-faculty.stanford.edu/\ensuremath{\sim}knuth}{Knuth},
where comments are given more importance than the code. It is not
restricted to Haskell alone; in fact \href{http://www.tug.org}{TeX} and
\href{http://en.wikipedia.org/wiki/METAFONT}{METAFONT} were written
first written by
\href{http://www-cs-faculty.stanford.edu/\ensuremath{\sim}knuth}{Knuth}
in a literate version Pascal language called
\href{http://en.wikipedia.org/wiki/WEB}{WEB} and later on ported to
\href{http://www-cs-faculty.stanford.edu/\ensuremath{\sim}knuth/cweb.html}{CWEB},
a literate version of the C Programming language. The \texttt{ghc}
compiler and the \texttt{ghci} interpreter supports both the literate
and non-literate version of Haskell.

Normally any line is treated as part of the program unless the
commented. In
\href{http://www.haskell.org/haskellwiki/Literate\_programming}{literate
haskell} all lines other than

\begin{enumerate}[1.]
\item
  Those that start with a `\textgreater{}' or
\item
  A block of lines enclosed in a \texttt{\textbackslash{}begin\{code\}}
  \texttt{\textbackslash{}end\{code\}}
\end{enumerate}
are treated as comments. We will use this literate style of programming;
we will use only the first form i.e.~program lines start with a
\texttt{\textgreater{}}. The advantage is that one can download the
notes directly and compile and execute them.

\section{Wild card patterns.}

We now illustrate the use of wild card patterns. Consider the function
that tests whether a list is empty. This can be defined as follows.

\begin{verbatim}

> null [] = True
> null _  = False
\end{verbatim}
The pattern \texttt{\_} (under score) matches any expression just like a
variable pattern. However, unlike a variable pattern where the matched
value is bound to the variable, a wild card discards the value. This can
be used when we do not care about the value in the RHS of the equation.

\section{Tuples and pattern matching.}

Besides lists Haskell supports the tuple type. Tuple types corresponds
to taking set theoretic products. For example the tuple
\texttt{(1,"Hello")} is an ordered pair consisting of the integer
\texttt{1} and the string \texttt{"Hello"}. Its type is
\texttt{(Int,String)} or equivalently \texttt{(Int,{[}Char{]})} as
\texttt{String} is nothing but \texttt{{[}Char{]}}. We illustrate the
pattern matching of tuples by giving the definition of the standard
functions \texttt{curry} and \texttt{uncurry}.

\section{A brief detour on currying}

In haskell functions are univariate functions unlike other languages.
Multi-parameter functions are captured using the process called
\emph{currying}. A function taking two arguments \texttt{a} and
\texttt{b} and returning \texttt{c} can be seen as a function taking the
\texttt{a} and returning a function that takes \texttt{b} and returning
\texttt{c}. This kind of function is called a \texttt{curried} function.
Another way in which we can represent a function taking 2 arguments is
to think of the function as taking a tuple. This is its uncurried form.
We now define the higher order functions that transforms between these
two forms.

\begin{verbatim}

> curry    :: ((a,b) -> c)  -> a -> b -> c
> uncurry  :: (a -> b -> c) -> (a,b) -> c
>
> curry   f a b   = f (a,b)
> uncurry f (a,b) = f a b
\end{verbatim}
The above code clearly illustrates the power of Haskell when it comes to
manipulating functions. Use of higher order functions is one of the
features that we will find quite a bit of use.

\section{Summary}

In this lecture we saw

\begin{enumerate}[1.]
\item
  Pattern matching for lists,
\item
  Tuples and pattern matching on them,
\item
  Literate haskell
\item
  Higher order functions.
\end{enumerate}

\end{document}
