\item still have \textbf{side effects} everywhere: one object changes the state of another and vice versa, may arbitrarily write to the hard drive, do kernel calls or launch a missle
Some parts of the implicit state machine have been made explicit by modelling classes, but it's still there and we have to deal with it, because we are modelling everything around states. Wouldn't it be nice if we could just forget about the global state machine? Maybe there is even a way to remove side effects and have more "predictability"?
\onslide<+->
We are lucky. There is. It's called \textbf{Haskell}.
Haskell is a \textbf{functional}, \textbf{pure}, \textbf{lazy} and \textbf{statically typed} high-level programming language.\\
\onslide<+->
\vspace{\baselineskip}
A few of the following facts will be white lies. But let's not worry about that. It's maybe more about "how to think in terms of haskell?" than "what is haskell?".
In haskell expressions are not evaluated until their results are actually needed. That has a lot of consequences, a few of them being:
\onslide<+->
\begin{itemize}[<+->]
\item infinite data structures are now possible (recursive and non-recursive)
\item defining new control structures by just defining a function (since not everything is evaluated... who needs if-then-else anyway?)
\item important for compositional programming and efficiency
\item laziness causes (memory) overhead, but most of the time the benefits outweigh the costs
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{What does \textbf{statically typed} mean?}
Every haskell expression has a type and types are always checked at \emph{compile-time}. Programs with errors will not compile and definitely not run.\\
\vspace{\baselineskip}
It is possible to simulate dynamic types however. In the end, they are still statically type-checked (as in: the compiler always knows what's going on).
Let's make our first function. We want something like the following mathematical function\\
$f(x)= x * x$\\
\vspace{\baselineskip}
How could the haskell code look like?
\pause
Almost the same:
\begin{lstlisting}
f x = x * x
\end{lstlisting}
\vspace{\baselineskip}
GHCi...\\
\pause
\vspace{\baselineskip}
What is a possible type signature for this function?
\begin{lstlisting}
f :: Int -> Int
f x = x * x
\end{lstlisting}
So the function gets an Int and returns an Int. Don't get confused by "\verb|->|". Just think of it as a symbol for separating input and output.
\end{frame}
\begin{frame}[fragile]
\frametitle{Functions and control structures (ctn.)}
In haskell we often use \textbf{pattern matching}. That means we define a function multiple times, but e.g. for different values of its input parameters. Let's see:
\pause
\setHaskellCodeStyle
\vspace{\baselineskip}
\begin{lstlisting}
isZero :: Int -> Bool
isZero 0 = True
isZero x = False
\end{lstlisting}
\vspace{\baselineskip}
So if we pass it 0, we get True. If we do not pass 0, we get False and the value we passed gets basically ignored.\\
\vspace{\baselineskip}
What might happen if we remove the second or the third line? What is a \textbf{partial function} and a \textbf{total function}?
\end{frame}
\begin{frame}[fragile]
\frametitle{Functions and control structures (ctn.)}
How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
These \verb#|# things above are called \textbf{guards} and are similar to \emph{pattern matching}. They are processed in order. If the condition on the left side of the equation is true, then it returns what stands on the right side of the equation. If it's false, then it processes the next line.\\\code{otherwise} on the last line is just defined as \code{True}, to make these constructs reasier to read and catch all other cases of input.
Note: we use \code{(x, y)} notation for both the type and the definition! Those are still two different things. We can also have triples, quadruples etc.
\end{frame}
\begin{frame}[fragile]
\frametitle{Lists}
\setHaskellCodeStyle
The list is probably the most basic data structure in Haskell. Like the array in C. It is a singly-linked list and is very lazy. The compiler has numerous ways to optimize lists, so don't be afraid to use them, even for huge things.
\pause
We build lists by using either the \code{[]} notation:
\begin{lstlisting}
list1 :: [Integer]
list1 = [1, 2]
\end{lstlisting}
\pause
or by using the \emph{cons} operator \code{(:)} which takes an element and a list and produces a new list with the element prepended to the front.
\begin{lstlisting}
list2 = 1 : []
-- is this really a list?
list3 = [1, 2] == 1 : 2 : []
emptyList = []
\end{lstlisting}
\pause
How about something more interesting:
\begin{lstlisting}
infiniteList = [1..]
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lists (ctn.)}
Again, we can do pattern matching on lists.
\setHaskellCodeStyle
\begin{lstlisting}
listLen :: [Integer] -> Integer
listLen [] = 0
listLen (x:xs) = 1 + listLen xs
\end{lstlisting}
We can also nest pattern matching:
\begin{lstlisting}
sumEveryTwo :: [Integer] -> [Integer]
sumEveryTwo [] = 0
sumEveryTwo (x:[]) = [x]
sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
\end{lstlisting}
Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
\end{frame}
\begin{frame}
\frametitle{Toolchain}
You need:
\begin{itemize}
\item\textbf{GHC}: this is the Haskell compiler
\item\textbf{GHCi}: this an interactive environment of GHC, similar to the interactive ruby shell \emph{irb}
\item\textbf{The Haskell Platform}: a collection including GHC, GHCi and basic libraries
\end{itemize}
Go to \url{https://www.haskell.org/platform}\\
For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}