\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{array}
% This is needed because raggedright in table elements redefines \\:
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\let\PBS=\PreserveBackslash
\usepackage[breaklinks=true,unicode=true]{hyperref}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
%\setcounter{secnumdepth}{0}


\begin{document}

\setcounter{chapter}{1}

\chapter{Warming up}

We will start with the haskell interpreter \texttt{ghci}. To start the
interpreter type the following on the command line. You might see
something like this.

\begin{verbatim}
$ ghci
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude>
\end{verbatim}
The \texttt{Prelude\textgreater{}} is the prompt and the interpreter is
expecting you to type in stuff. What you can type is any valid haskell
expression. You can for example use ghci like a calculator.

\begin{verbatim}
Prelude> 42
42
Prelude> 2 + 4
6
Prelude> sqrt 2
1.4142135623730951
\end{verbatim}
The line 5 \texttt{sqrt 2} is a \emph{function application}, the
function sqrt is applied on 2. In haskell, applying a function
\texttt{f} on an argument \texttt{x} is done by placing \texttt{f} on
the left of \texttt{x}. Function application associates to the left,
i.e. \texttt{f x y z} means \texttt{((f x) y)z}.

\section{Types}

Although it did not appear to be the case in this limited interaction,
Haskell is a strongly typed language unlike python or scheme. All values
in haskell have a type. However we can almost always skip types. This is
because the compiler/interpreter infers the correct type of the
expression for us. You can find the type of an expression prefixing it
with `:type' at the prompt. For example

\begin{verbatim}
Prelude> :type 'a'
'a' :: Char
\end{verbatim}
In haskell the a type of a value is asserted by using \texttt{::} (two
colons). The interpreter just told us that \texttt{'a'} is an expression
of type \texttt{Char}.

Some basic types in Haskell are given in the table below

\ctable[pos = H, center, botcap]{ll}
{% notes
}
{% rows
\FL
Haskell type & What they are
\ML
Bool & Boolean type
\\\noalign{\medskip}
Int & Fixed precision integer
\\\noalign{\medskip}
Char & Character
\\\noalign{\medskip}
Integer & Multi-precision integer
\LL
}

\section{Lists and Strings.}

A list is one of the most important data structure in Haskell. A list is
denoted by enclosing its components inside a square bracket.

\begin{verbatim}
Prelude> :type ['a','b']
['a','b'] :: [Char]
Prelude> :type "abcde"
"abcde" :: [Char]
\end{verbatim}
A string in Haskell is just a list of characters. The syntax of a string
is exactly as in say C with escapes etc. For example
\texttt{"Hello world"} is a string.

Unlike in other languages like python or scheme, a list in Haskell can
have only one type of element, i.e. \texttt{{[}1, 'a'{]}} is \emph{not}
a valid expression in haskell. A list of type \texttt{t} is denoted by
\texttt{{[}t{]}} in haskell. Example the type \texttt{String} and
\texttt{{[}Char{]}} are same and denote strings in Haskell.

\section{Functions.}

In haskell functions are first class citizens. They are like any other
values. Functions can take functions as arguments and return functions
as values. A function type is denoted using the arrow notation, i.e.~A
function that takes an integer and returns a character is denoted by
\texttt{Integer -\textgreater{} Char}.

\begin{verbatim}
Prelude> :type length
length :: [a] -> Int
\end{verbatim}
Notice that the interpreter tells us that length is a function that
takes the list of \texttt{a} and returns an \texttt{Int} (its length).
The type \texttt{a} in this context is a \emph{type variable}, i.e.~as
far as length is concerned it does not care what is the type of its
list, it returns the length of it. In Haskell one can write such generic
functions. This feature is called polymorphism. The compiler/interpreter
will appropriately infer the type depending on its arguments

\begin{verbatim}
Prelude> length [1,2,3]
3
Prelude> length "Hello"
5
\end{verbatim}
Polymorphism is one of the greatest strengths of Haskell. We will see
more of this in time to come.

Let us now define a simple haskell function.

\begin{verbatim}
fac 0 = 1
fac n = n * fac (n - 1)
\end{verbatim}
Save this in a file, say \texttt{fac.hs} and load it in the interpreter

\begin{verbatim}
$ ghci fac.hs
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling Main             ( fac.hs, interpreted )
Ok, modules loaded: Main.
Main> fac 0
1
Main> fac 4
24
\end{verbatim}
A haskell function is defined by giving a set of equations. A function
equation looks like

\begin{verbatim}
f pat_1_1 pat_1_2 ... pat_1_n  = expr_1
f pat_2_1 pat_2_2 ... pat_2_n  = expr_2
          ...
f pat_m_1 pat_2_2 ... pat_m_n  = expr_m
\end{verbatim}
The formal parameters can be \emph{patterns}. We will define what
patterns in detail latter on but a constant like \texttt{0} or a
variable like \texttt{n} is a pattern. When a function is evaluated, its
actual arguments are \emph{matched} with each of the equations in turn.
We cannot explain what matching means in full detail here because we
have not explained patterns yet. However, for constant patterns like
\texttt{0} or a variable it is easy. An actual argument can match a
constant if and only if the argument evaluates to that constant. On the
other hand a variable pattern matches any argument. If a function
\texttt{f} is defined using $n$ equations, then while evaluating the
function on a set of arguments each equation is tried out in order. The
first equation of \texttt{f} whose formal parameters match the actual
argument is used for evaluation.

To illustrate we consider the function \texttt{fac} that we defined. The
expression \texttt{fac 0} evaluates to 1 because the actual parameter
matches with the formal parameter 0 in the first equation. On the other
hand, in the expression \texttt{fac 2} the actual argument \texttt{2}
cannot match the formal argument 0 in the first equation of
\texttt{fac}. Therefore the next equation is tried namely
\texttt{fac n = n * fac (n-1)}. Here the formal parameter \texttt{n}
matches 2 (recall a variable pattern can match any argument). Therefore,
\texttt{fac 2} evaluates to \texttt{2 * fac (2 - 1)} which by recursion
evaluates to 2.

We give two alternate defintions of the factorial function, the first
using \emph{guards} and the next using \texttt{if then else}.

\begin{verbatim}
fac1 n | n == 0      = 1
       | otherwise   = n * fac1 (n -1)

fac2 n = if n == 0 then 1 else n * fac2 (n -1)
\end{verbatim}
To summarise

\begin{enumerate}[1.]
\item
  Haskell functions are polymorphic
\item
  They can be recursive.
\item
  One can use pattern matching in their definition.
\end{enumerate}

\end{document}
