\documentclass{beamer}
\usepackage{listings}

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

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

\frame{\titlepage}

\begin{frame}
  \frametitle{Welcome!}

  A few things to mull over:
  \begin{itemize}
  \item Our pace will be fairly rapid.
  \item Stop me and ask questions---early and often.
  \item I assume \emph{no} prior Haskell or functional programming
    exposure.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{What's software, doc?}
  
  What does a program do?

  \begin{itemize}
  \item It consumes \emph{old data}.
  \item It computes over the old data.
  \item It produces \emph{new data}.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{A familiar program}

  Consider the Unix \texttt{sort} command:

  \begin{itemize}
  \item Consumes (possibly unsorted) lines of text.
  \item Sorts the lines.
  \item Produces sorted lines of text.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Have you ever run \texttt{sort}?}

  If so, you're already a functional programmer.
\end{frame}

\begin{frame}
  \frametitle{What's functional programming?}
  
  \begin{definition}
    Functional programming is a style of programming that emphasizes
    \alert{functions} and \alert{function application}.
  \end{definition}
\end{frame}

\begin{frame}
  \frametitle{What's a function?}

  \begin{definition}
    A function \alert{inspects} its \alert{input}, and
    \alert{produces} an \alert{output}.
  \end{definition}

  \begin{definition}
    Function \alert{application} involves supplying a function with
    one or more arguments (inputs).
  \end{definition}
\end{frame}

\begin{frame}
  \frametitle{Why is the \texttt{sort} command functional?}
  
  Discuss.
\end{frame}

\begin{frame}
  \frametitle{So what?}
  
  I can already write code that inspects inputs and produces outputs
  in C, Python, or whatever. 

  \vskip12pt
  So what's \emph{different} about functional programming (FP)?

  \begin{itemize}
  \item We only \emph{inspect} inputs; we don't \emph{change} them.
  \item In fact, we generally don't change \emph{any} data.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{What's interesting about functional programming?}
  
  I'll tell you what got me hooked:
  \begin{itemize}
  \item It requires a \emph{substantially different} way of thinking
    about programming.
  \item In exchange for the challenge, it offers great expressiveness
    and conciseness.
  \item It is beautiful.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Why Haskell?}
  
  Why choose Haskell over Erlang, Clojure, F\#, OCaml, \ldots?
  \begin{itemize}
  \item It is the most radical of the practical functional languages.
  \item It will force you to learn many new things at once,
  \item and to \emph{unlearn} things that perhaps you thought you knew.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Getting started with Haskell}
  
  We'll be using the Haskell Platform:
  \begin{itemize}
  \item \url{http://hackage.haskell.org/platform/}
  \end{itemize}

  \vskip12pt
  What is this Platform, anyway?
  \begin{itemize}
  \item A modern, optimising compiler (\texttt{ghc})
  \item An interactive interpreter (\texttt{ghci})
  \item Useful standard libraries
  \item An awesome package manager (\texttt{cabal})
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Starting with interaction}
  
  The interactive interpreter for the Haskell Platform is named
  \texttt{ghci} (for ``GHC interactive'').  When you run it, you'll
  see something like this:

\begin{verbatim}
GHCi, version 6.10.3: http://www.haskell.org/ghc/
  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> 
\end{verbatim}

  That ``\texttt{Prelude>}'' is the prompt.
\end{frame}

\begin{frame}
  \frametitle{Really simple stuff}
  
  What happens when you enter these expressions at the \texttt{ghci}
  prompt?
  \begin{itemize}
  \item \lstinline{2+2}
  \item \lstinline{putStrLn "hi mom!"}
  \item \lstinline{"foo" ++ "bar"}
  \item \texttt{:quit}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Handy things to know}
  
  \begin{itemize}
  \item You should be able to use your arrow keys to edit your
    \texttt{ghci} input.
  \item Up and down arrows should go through your input history.
  \item Tab should complete names: try typing
    \texttt{put<TAB>}.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Haskell source files}

  \begin{itemize}
  \item The standard Haskell source file extension is ``\texttt{.hs}''.
  \item By convention, files use \texttt{InterCapsNaming}.
  \item Save the following in a file named \texttt{Hello.hs}:
  \lstinputlisting{1/Hello.hs}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Run your program immediately}
  
  \begin{itemize}
  \item We can run our new program straight away, via the
    \texttt{runghc} batch interpreter:
\begin{verbatim}
$ runghc Hello.hs
hello, world!
\end{verbatim}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Compilation}
  
  \begin{itemize}
  \item We can also compile our new program to a native executable:
\begin{verbatim}
$ ghc --make Hello
[1 of 1] Compiling Main   ( Hello.hs, Hello.o )
Linking Hello ...
\end{verbatim}
  \item (GHC's ``\texttt{--make}'' option is super-useful!)
  \item We can run our new program in the usual way:
\begin{verbatim}
$ ./Hello
hello, world!
\end{verbatim}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Operators and things}

  Haskell's rules for writing expressions with operators should look
  familiar.

\begin{lstlisting}
b^2 - 4*a*c
\end{lstlisting}

  \begin{itemize}
  \item The \lstinline{^} operator is (integral) exponentiation.
  \item Expressions group as you might expect, i.e.
    \lstinline{(b^2) - (4*a*c)}.
  \end{itemize}

  Confused about operator precedence? Parens are your friend!
\end{frame}

\begin{frame}
  \frametitle{What about functions?}
  
  To apply a function to some arguments, we simply write it next to
  its arguments.

  \begin{itemize}
  \item \lstinline{length "hello"}
  \end{itemize}

  Function application binds tighter than operations, so if you write
  this:
  \begin{itemize}
  \item \lstinline{sqrt 2 - 3}
  \end{itemize}
  What you'll get is this:
  \begin{itemize}
  \item \lstinline{(sqrt 2) - 3}
  \end{itemize}
  If you mean something else, tell the compiler!
  \begin{itemize}
  \item \lstinline{sqrt (2 - 3)}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{A more complex expression}
  
  Look familiar?
  \begin{itemize}
  \item \lstinline{(-b + sqrt (b^2 * 4*a*c)) / (2*a)}
  \end{itemize}
  (It's part of the solution to a quadratic equation.)
\end{frame}

\begin{frame}[fragile]
  \frametitle{Expressions are all very well}
  
  But we need a bit more to do anything useful.
  \begin{itemize}
  \item We have to be able to give things names.
  \end{itemize}

  To define something, we supply a name, an equals sign, and an
  expression.
\begin{lstlisting}
e = 2.718281828459045
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A more useful definition}
  
  When we supply more than one name before the equals sign, the
  remainder are the arguments to a function.
\begin{lstlisting}
oneRoot a b c = (-b + (b^2 + 4*a*c)) / (2*a)
\end{lstlisting}
  We've now defined a function named \lstinline{oneRoot}, with three
  arguments.
  \begin{itemize}
  \item Try saving this definition into a source file, then load it
    into \texttt{ghci} and use it.
  \item What's the result of this expression in \texttt{ghci}?
\begin{lstlisting}
oneRoot 2 3 4
\end{lstlisting}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Your turn!}
  
  I'm going to open up an Emacs window.

  \begin{itemize}
  \item Tell me how to write a function that computes the mean of
    three numbers.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Speaking of Emacs}
  
  What are the best tools for working with Haskell source code?
  \begin{itemize}
  \item Emacs
  \item vim
  \end{itemize}
  Google for ``emacs haskell mode'' or ``vim haskell mode'' for
  details.

  \vskip8pt
  Do you prefer IDEs?
  \begin{itemize}
  \item At least for now, you've come to the wrong language.  Back to
    Visual Studio with you!
  \item Less flippant answer: there are no mature Haskell IDEs yet.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Of source files and editors}
  
  Haskellers hate tab characters!  (For the same reason Python
  programmers do: they're unportable.)

  \vskip12pt
  A decent Haskell mode for a good editor will give you some useful
  features:
  \begin{itemize}
  \item Syntax highlighting.
  \item Intelligent indentation.
  \item Compilation and jump-to-error support.
  \item \emph{No tabs}!
  \item Maybe interaction with \texttt{ghci}.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{A very tiny, marginally useful program}
  
  \lstinputlisting{1/SimpleWordCount.hs}

  \vskip12pt
  What's going on here?
  \begin{itemize}
  \item The \lstinline{words} function breaks up a text string into a
    list of words.
  \item The \lstinline{length} function\ldots\,well, you can guess.
  \item The \lstinline{show} function takes something (in this case, a
    number) and represents it as a string.
  \item The \lstinline{++} operator means ``append.''
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Oh, and then there's interact}
  
  The \lstinline{interact} function is your gateway to Unix shell
  pipeline bliss.  It:
  \begin{itemize}
  \item reads from standard input;
  \item feeds it to your function as a string;
  \item consumes the string that your function produces; and
  \item prints it to standard output.
  \end{itemize}
  
\end{frame}

\begin{frame}
  \frametitle{Another very tiny program}
  
  I need your help!

  \vskip12pt
  Who can suggest what's going on here?

  \vskip24pt
  \lstinputlisting{1/SimpleSort.hs}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Figuring stuff out}
  
  When you're in exploratory mode (i.e.~most of the time, at least for
  me), \texttt{ghci} is your friend.

  \vskip8pt
  Wonder what \lstinline{lines} does?  Simply try it, and find out!
\begin{semiverbatim}
Prelude> lines "foo\\nbar\\nquux\\n"
["foo","bar","quux"]
\end{semiverbatim}

  What else did we find out here?
\end{frame}

\begin{frame}[fragile]
  \frametitle{Lists}
  
  The syntactic sugar for a list of items is square brackets
  surrounding items, which are separated by commas:
\begin{lstlisting}
[4,8,15,16,23,42]
[True, False]
["Linden", "Lab"]
\end{lstlisting}

  \vskip8pt
  Let's try some experiments with lists.
  \begin{itemize}
  \item Can we write this? \lstinline{[7,True]}
  \item What about this? \lstinline{[[1,2],[3,4]]}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{More experimentation}
  
  \begin{itemize}
  \item What does the \lstinline{unlines} function do?
  \item What about \lstinline{sort}?
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Another small program}
  
  Let's develop something!  Another Unix-like command!

  \vskip12pt
  We'll write a program that prefixes each line of input with a line
  number.

  \vskip12pt
  Okay\ldots\,so now what?
\end{frame}

\begin{frame}
  \frametitle{When in doubt}
  
  If I don't know how to solve a problem, I like to start by seeing if
  there are bits of it I know how to solve.

  \vskip12pt
  What do we already know about?
  \begin{itemize}
  \item Defining new functions!
  \item And, of course, using existing functions! 
  \item We have a small library of built-in functions and operators,
    e.g.~\lstinline{show}, \lstinline{unlines}, and \lstinline{++}.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{The small problem}
  
  Given one number and one line of text, how should we render them?

  \vskip12pt
  \pause
\begin{lstlisting}
oneNumber n s = show n ++ ": " ++ s
\end{lstlisting}

  \pause
  \vskip12pt
  Let's try this function in \texttt{ghci}:
\begin{verbatim}
Prelude> oneNumber 3 "blargh"
"3: blargh"
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Powering up}
  
  That's a good start. But now what? 

  \vskip12pt
  We need to produce a whole series of lines with numbers.

  \vskip12pt
  \pause
\begin{lstlisting}
lineNumbers n xs =
    if xs == []
    then   []
    else   oneNumber n (head xs) 
         : lineNumbers (n+1) (tail xs)
\end{lstlisting}

  \vskip12pt
  \pause
  \alert{Hey!} See that little lonely ``\lstinline{:}''? It's a
  \emph{list constructor} (like \texttt{cons} in Lisp or Scheme).
\end{frame}

\begin{frame}[fragile]
  \frametitle{The quantum leap}
  
  Wow, we just did something interesting:
  \begin{itemize}
  \item We wrote a \emph{recursive} function.
  \end{itemize}
  Not bad for the first lecture!
  \pause
  \vskip12pt
  We still lack a little glue to create a complete program.
\pause
\vskip12pt
\begin{lstlisting}
bunchie xs = unwords (lineNumbers 1 (lines xs))

main = interact bunchie
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Accompanying materials}
  
  Where should you be going to learn more?
  \begin{itemize}
  \item \url{http://book.realworldhaskell.org/}
  \item \url{http://learnyouahaskell.com/}
  \item \url{http://haskell.org/}
  \item \url{http://slideshare.net/bos31337}
  \item \texttt{\#haskell} on \texttt{irc.freenode.net} (I'm
    \texttt{bos})
  \end{itemize}

  \pause
  \vskip12pt
  Source code for these slides and examples:
  \begin{itemize}
  \item \texttt{darcs get} \url{http://darcs.serpentine.com/rwh-course}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Recap}
  What have we covered today?
  \begin{itemize}
  \item What functional programming is.  A tiny bit about Haskell.
  \item The Haskell Platform, and why it matters.
  \item Writing expressions, and defining functions.
  \item Simple interactive Unix-like commands.
  \end{itemize}

  \pause
  \vskip18pt
  There \emph{will} be homework.  Assignments will go out later today
  to the \texttt{haskell} mailing list.

  \pause
  \vskip18pt
  Thanks!  Questions?
\end{frame}
\end{document}

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

