\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{url}
\usepackage[breaklinks=true,unicode=true]{hyperref}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
%\setcounter{secnumdepth}{0}


\begin{document}

\setcounter{chapter}{3}

\chapter{The Sieve of Eratosthenes.}

In this lecture, we look at an algorithm to find primes which you might
have learned in school. It is called the
\href{http://en.wikipedia.org/wiki/Sieve\_of\_Eratosthenes}{Sieve of
Eratosthenes}. The basic idea is the following.

\begin{enumerate}[1.]
\item
  Enumerate all positive integers starting from 2.
\item
  Forever do the following
\item
  Take the smallest of the remaining uncrossed integer say $p$ and
  circle it.
\item
  Cross out all numbers that are the factors of the circled integer $p$.
\end{enumerate}
All the circled integers are the list of primes. For an animated
description see the wiki link
\url{http://en.wikipedia.org/Sieve_of_Eratosthenes}.

We want to convert this algorithm to haskell code. Notices that the
Sieve seperates the primes from the composites. The primes are those
that are circled and composites are those that are crossed. So let us
start by defining

\begin{verbatim}

> primes = circledIntegers
\end{verbatim}
i.e.~the primes are precisely the list of circled integers.

An integer gets circled if and only if it is not crossed at any stage of
the sieving process. Furthermore, a integer gets crossed in the stage
when its least prime factor is circled. So to check whether an integer
is crossed all we need to do is to check whether there is a prime which
divides it.

\begin{verbatim}

> divides x n = n `mod` x == 0
>
> check (x:xs) n | x * x > n      = True       -- the rest are bigger.
>                | x `divides` n  = False      -- We hit a factor
>                | otherwise      = check xs n -- need to do more work
>
> isCircled = check primes
>
\end{verbatim}
The function \texttt{isCircle x} checks whether \texttt{x} will
eventually be circled. One need not go over all primes as the smallest
non-trivial prime $p$ that divides a composite number $n$ should always
be less than or equal to $\sqrt{n}$. This explains the first guard
statement of the check function.

\section{Guards detour.}

We now explain another feature of Haskell namely guards. Look at the
definition of the function \texttt{check}. Recall that a function is
defined by giving a set of equations. For each such equation, we can
have a set of guards. The syntax of these guarded equation looks like

\begin{verbatim}
f p_1 ... p_n | g_1  = e_1
              | g_2  = e_2
              | g_3  = e_3
              | ...
              | g_m  = e_m
\end{verbatim}
Each of the guards \texttt{g\_i} is a boolean expression. You should
read this as ``if \texttt{f}'s arguments match the patterns
\texttt{p1 ... pn} then its value is \texttt{e\_1} if \texttt{g\_1} is
true, \texttt{e\_2} if \texttt{g\_2} is true, etc \texttt{e\_m} if
\texttt{g\_m} is true''. If multiple guards are true then the guard
listed first has priority. For example consider the following function

\begin{verbatim}

>
> f x | x >= 0 = "non-negative"
>     | x <= 0 = "negative"
\end{verbatim}
Then \texttt{f 0} is the string \texttt{"non-negative"}. If you want to
add a default guard, then use the keyword \texttt{otherwise}. The
keyword \texttt{otherwise} is nothing but the boolean value
\texttt{True}. However, in guards it is a convention to write
\texttt{otherwise} instead of \texttt{True}.

Finally, we want to define the list of \texttt{circledInteger}. Clearly
the first integer to be circled is 2. The rest are those integers on
which the function \texttt{isCircled} returns true. And here is the
Haskell code.

\begin{verbatim}

>
> circledIntegers = 2 : filter isCircled [3..]
>
\end{verbatim}
Here filter is a function that does what you expect it to do. Its first
argument is a predicate, i.e it take an element and returns a boolean,
and its second argument is a list. It returns all those elements in the
list that satisfies the predicate. The type of filter is given by
\texttt{filter :: (a -\textgreater{} Bool) -\textgreater{} {[}a{]} -\textgreater{} {[}a{]}}.
This completes the program for sieving primes. Now load the program into
the interperter

\begin{verbatim}
$ ghci Sieve-of-Eratosthense.lhs
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             ( src/lectures/Sieve-of-Eratosthenes.lhs, interpreted )
Ok, modules loaded: Main.
*Main> :type take
take :: Int -> [a] -> [a]
*Main> take 10 primes
[2,3,5,7,11,13,17,19,23,29]
\end{verbatim}
The \texttt{take n xs} returns the first \texttt{n} elements of the list
\texttt{xs}.

\section{How is the circularity of primes handled?}

One thing you might have noticed is that the list \texttt{primes} has a
circular definition. The compiler is able to grok this circularity due
to the fact that Haskell is a lazy language. No expression is evaluated
unless it is required. For each integer in the list \texttt{primes} to
decide that it is circled, we need to consider only the primes that are
less than its square root. As a result the definition of primes does not
blow up into an infinitary computation.

\section{Infix application of function and prefix application of
operators.}

We now explain another feature of a Haskell. Notice the use of
\texttt{mod} in the expression n `mod` x == 0 in the definition of the
function \texttt{divides} and in the guard x `divides` n in the
definition of the function \texttt{check}. We have used a two argument
function (i.e.~its type is
\texttt{a -\textgreater{} b -\textgreater{} c}) like an operator. For
any such function \texttt{foo} we can convert it into a binary operator
by back quoting it, i.e. `foo`.

The converse is also possible, i.e.~we can convert an operator into the
corresponding function by enclosing it in a bracket. For example the
expression \texttt{(+)} (there is no space in side the bracket otherwise
it is an error) is a function
\texttt{Int -\textgreater{} Int -\textgreater{} Int}.

\begin{verbatim}

> incr1 = map ((+) 1)
> incr2 = map (+1)
\end{verbatim}
The two function \texttt{incr1} and \texttt{incr2} both does the same
thing; increments all the elements in a list of integers by 1.

\end{document}
