\documentclass{beamer}
\usepackage{amsmath}
\usepackage{listings}

\title{Real World Haskell:\\
  Lecture 2}
\author{Bryan O'Sullivan}
\date{2009-10-14}

\begin{document}
\lstset{language=Haskell}

\frame{\titlepage}

\begin{frame}
  \frametitle{My homework, using only concepts from last week}
  
  \lstinputlisting{2/Homework.hs}
\end{frame}

\begin{frame}
  \frametitle{Wasn't Haskell supposed to be ``pretty''?}
  
  That \lstinline{grep} function sure didn't look pretty to me!

  \vskip12pt
  But what, specifically, is ugly about it?
  \begin{itemize}
  \item We \emph{repeat ourselves}, using \lstinline{head} and
    \lstinline{tail} twice.
  \item There's a mess of nested \lstinline{if}/\lstinline{else}
    badness going on.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Lists, revisited}
  
  There are two ways to construct a list:
  \begin{itemize}
  \item An empty list\\\lstinline{[]}
  \item A non-empty list\\\lstinline{firstElement : restOfList}
  \end{itemize}

  \vskip12pt
  We refer to \lstinline{[]} and \lstinline{:} as \emph{list
    constructors}, since they construct list values.
\end{frame}

\begin{frame}
  \frametitle{Lists, constructed}

  Knowing about these constructors, how might we construct a 4-element
  list?  \pause
  \begin{itemize}
  \item \lstinline{1 : 2 : 3 : 4 : []}
  \end{itemize}

  \vskip12pt
  The bracketed notation we saw last week is \emph{syntactic sugar}
  for the form above.

  \vskip12pt
  In other words, any time you see \emph{this}:
  \begin{itemize}
  \item \lstinline{[1,2,3,4]}
  \end{itemize}
  You can read it as \emph{this}, and vice versa:
  \begin{itemize}
  \item \lstinline{1 : 2 : 3 : 4 : []}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Lists, misconstrued}

  \alert{Beginner mistake alert:}\\
  A list \emph{must} end with an empty list.  So a construction like
  this makes no sense:
  \begin{itemize}
  \item \lstinline{'a' : 'b' : 'c'}
  \end{itemize}

  \vskip12pt
  How would we fix it up?
  \begin{itemize}
  \item \lstinline{'a' : 'b' : 'c' : []}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Back to our roots}
  
  Remember the fragment of square root code from last week?
\begin{lstlisting}
oneRoot a b c = (-b + (b^2 + 4*a*c)) / (2*a)
\end{lstlisting}

  \vskip12pt
  If we pass in a value of zero for \lstinline{a}, the root is
  undefined, since we'd be dividing by zero.

  \vskip12pt
\begin{lstlisting}
oneRoot a b c = if a == 0
                then (-b + (b^2 - 4*a*c)) 
                     / (2*a)
                else error "divide by zero!"
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{But...}
  
  I don't like that \lstinline{if}, because how would we write this
  using mathematical notation?

  \begin{align*}
    \textrm{roots}(a,b,c) & = \frac{-b \pm (b^{2} - 4ac)}{2a}& \textrm{if $a \neq 0$} \\
    & = \textrm{undefined} &\textrm{otherwise}
  \end{align*}

  And \ldots\,isn't Haskell supposed to be mathematically inspired?
\end{frame}

\begin{frame}[fragile]
  \frametitle{Introducing guards}

  A \emph{guard} is a Boolean expression preceded by a vertical bar
  character.
  \vskip12pt
\begin{lstlisting}
oneRoot a b c
  | a /= 0    = (-b + (b^2 - 4*a*c)) / (2*a)
  | otherwise = error "divide by zero"
\end{lstlisting}

  \vskip12pt
  \begin{itemize}
  \item Guards are evaluated in top-to-bottom order.
  \item For the first one that evaluates to \lstinline{True}, the
    expression on the right of the \lstinline{=} sign is used as the
    result of the function.
  \item The name \lstinline{otherwise} is simply another name for
    \lstinline{True}.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Using guards}

  Here's a second attempt at our \lstinline{grep} function, this time
  using guarded expressions:
  \lstinputlisting{2/Grep2.hs}
\end{frame}

\begin{frame}
  \frametitle{How did this help?}
  
  We got rid of the nested \lstinline{if} expressions, and our
  ``flatter'' code is easier to follow.

  \vskip12pt
  It's still fugly and repetitive, though.  What about
  \lstinline{head} and \lstinline{tail}?
\end{frame}

\begin{frame}
  \frametitle{Pattern matching}

  When we construct a list, the Haskell runtime has to remember what
  constructors we used.

  \vskip12pt
  It goes a step further, and makes this information available to
  \emph{us}.

  \vskip12pt 
  We can examine the structure of a piece of data at runtime using
  \emph{pattern matching}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Pattern matching on an empty list}
  
  What's the length of an empty list?

  \vskip12pt 
\begin{lstlisting}
myLength [] = 0
\end{lstlisting}

  \vskip12pt 
  This is a function of one argument.

  \vskip8pt
  If that argument matches the empty-list constructor, our function
  returns the value 0.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Pattern matching on a non-empty list}

  What's the length of a non-empty list?

  \vskip12pt 
\begin{lstlisting}
myLength (x:xs) = 1 + myLength xs
\end{lstlisting}

  \vskip12pt 
  If our argument matches the non-empty-list constructor
  ``\lstinline{:}'', then:
  \begin{itemize}
  \item the head of the list is bound to the name \lstinline{x};
  \item the tail to \lstinline{xs};
  \item and the expression is returned with those bindings.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Aaaand it's over to you}
  
  Now that we know how pattern matching works, let's do some
  super-simple exercises:

  \vskip12pt
  Write versions of the \lstinline{head} and \lstinline{tail}
  functions:
  \vskip8pt
\begin{lstlisting}
head  [1,2,3]
  ==> 1
tail  ['a','b','c']
  ==> ['b','c']
\end{lstlisting}

  \vskip12pt
  Give your versions different names, or you'll have a hard time
  trying them out in \texttt{ghci}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Matching alternative patterns}

  We combine our two pattern matches into one function definition by
  writing them one after the other:

  \vskip8pt
\begin{lstlisting}
myLength []     = 0
myLength (x:xs) = 1 + myLength xs
\end{lstlisting}

  \vskip8pt
  As with guards, pattern matching proceeds from top to bottom and
  stops at the first success.
  \begin{itemize}
  \item The RHS of the first pattern that succeeds is used as the body
    of the function.
  \end{itemize}
  
  \pause
  \vskip12pt
  \alert{Question:} What do you suppose happens if \emph{no} pattern
  matches?
\end{frame}

\begin{frame}[fragile]
  \frametitle{Over to you, part two}
  
  And now that we know how to write function definitions that can deal
  with multiple patterns, another exercise:

  \vskip12pt
  Write a version of the \lstinline{take} function:
  \vskip8pt
\begin{lstlisting}
take 3 [100,200,300,400,500]
  ==>  [100,200,300]
take 3 ['a','b']
  ==>  ['a','b']
take 3 []
  ==>  ???
\end{lstlisting}

  \pause
  \vskip12pt
  Now use \texttt{ghci} to figure out what the \lstinline{drop}
  function does, and write a version of that.
\end{frame}

\begin{frame}
  \frametitle{Metasyntactic variables}
  
  Languages have their cultural habits, and Haskell is no exception.

  \vskip12pt
  You'll very often see the names used when pattern matching a list
  follow a naming convention like this:
  \begin{itemize}
  \item \lstinline{(x:xs)}
  \item \lstinline{(y:ys)}
  \item \lstinline{(d:ds)}
  \end{itemize}
  and so on.

  \vskip12pt
  Think of the ``s'' suffix as ``pluralizing'' a name, so ``x''
  (\emph{ex}) is the head of the list, and ``xs'' (\emph{exes}) is the
  rest.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Matching multiple patterns}
  
  We can match more than one pattern at a time.

  \vskip12pt
  Consider how we might add the elements of two vectors, represented
  as lists:

  \vskip8pt
\begin{lstlisting}
sumVec (x:xs) (y:ys) = x + y : sumVec xs ys
sumVec []     []     = []
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Combining pattern matching and guards}
  
  Things start to get seriously expressive when we combine language
  features.

  \vskip12pt
  Remember that bloated \lstinline{grep} definition from earlier?
  Let's put our new friends to work!

  \vskip8pt
  \lstinputlisting{2/Grep3.hs}
\end{frame}

\begin{frame}
  \frametitle{What's happening here?}

  When we define a function, a pattern binds \emph{names} to
  \emph{values}. Given a list and a pattern \lstinline{(x:xs)}, if the
  list is non-empty, then \lstinline{x} is bound to its head, and
  \lstinline{xs} to its tail.

  \begin{itemize}
  \item Then each guard (if any) associated with that pattern is
    evaluated in turn, with those bindings in effect, until a guard
    succeeds.
  \item Once a guard succeeds, its RHS is used as the result, with the
    bindings from that pattern still in effect.
  \item If the pattern match fails, or no guard succeeds, we fall
    through to the next pattern and its guards.
  \end{itemize}

  \pause
  \vskip8pt
  \alert{Note}: If all patterns and guards in a function definition
  were to fail on some input, we'd get a runtime error. That would be
  bad.
\end{frame}

\begin{frame}[fragile]
  \frametitle{And speaking of bad\ldots}
  
  Remember our \lstinline{sumVec} function?
  \vskip8pt
\begin{lstlisting}
sumVec (x:xs) (y:ys) = x + y : sumVec xs ys
sumVec []     []     = []
\end{lstlisting}

  \vskip8pt
  What happens if we apply this to lists of different lengths?

  \vskip8pt
  \begin{itemize}
  \item \lstinline{sumVec [1,2,3] [4,5,6,7,8]}
  \end{itemize}

  \pause
  \vskip8pt
  So \ldots\,what can we do about that exciting behaviour?
\end{frame}

\begin{frame}[fragile]
  \frametitle{One possible response}
  
  Let's declare that the sum of two vectors should end when we reach
  the end of the shorter vector.

  \vskip8pt
\begin{lstlisting}
sumVec (x:xs) (y:ys) = x + y : sumVec xs ys
sumVec what   ever   = []
\end{lstlisting}
  \vskip8pt

  \emph{Whoa, dude}\ldots\,Why does this work?

  \pause
  \vskip8pt
  \begin{itemize}
  \item The names ``\lstinline{what}'' and ``\lstinline{ever}'' \emph{are}
    patterns.
  \item However, a \emph{plain name} (with no constructors in sight)
    \emph{does not inspect} the structure of its argument.
  \item So ``\lstinline{what}'' and ``\lstinline{ever}'' will each
    happily match \emph{either} an empty \emph{or} a non-empty list.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{An aside: strings are lists}
  
  In Haskell, we write characters surrounded by single quotes, and
  strings in double quotes.  Strings are lists, so:
  \begin{itemize}
  \item
\begin{lstlisting}
"abc"
\end{lstlisting}
  \end{itemize}
  is syntactic sugar for
  \begin{itemize}
  \item
\begin{lstlisting}
['a', 'b', 'c']
\end{lstlisting}
  \end{itemize}
  and hence for
  \begin{itemize}
  \item
\begin{lstlisting}
'a' : 'b' : 'c' : []
\end{lstlisting}
  \end{itemize}

  Functions that can manipulate lists can thus manipulate strings.

  Oh, and escape sequences such as \lstinline{"\r\n\t"} work, too.
\end{frame}

\begin{frame}[fragile]
  \frametitle{We are not limited to one constructor per pattern}
  
  Suppose we want to squish consecutive repeats of an element in a
  list.
\begin{lstlisting}
compress "fooobarrrrrr"
  ==> "fobar"
\end{lstlisting}

  \vskip12pt
  We can write a function to do this using an elegant combination of
  pattern matching and guards:
  \vskip8pt
\lstinputlisting{2/Compress.hs}

  \vskip8pt
  Notice that our pattern matches on two consecutive list
  constructors!
\end{frame}

\begin{frame}[fragile]
  \frametitle{Homework}
  
  \begin{itemize}
  \item Write a function that returns the $n$th element of a list,
    counting from zero.
\begin{lstlisting}
nth 2 "squeak"
  ==> 'u'
\end{lstlisting}
  \item Write a function that returns the element \emph{immdiately
      before} the last element of a list.
\begin{lstlisting}
lastButOne [1,2,3,4,5]
  ==> 4
\end{lstlisting}
  \item Write a function that determines whether its input is a
    palindrome.
\begin{lstlisting}
isPalindrome "foobar"
  ==> False
isPalindrome "foobarraboof"
  ==> True
\end{lstlisting}
  \end{itemize}
\end{frame}

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% TeX-PDF-mode: t
%%% End: 

