You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

437 lines
14 KiB

  1. \documentclass[10pt,a5paper,mathserif,serif,usenames,dvipsnames]{beamer}
  2. % packages
  3. \usepackage{xcolor}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage{amsmath}
  6. \usepackage{amsfonts}
  7. \usepackage{amssymb}
  8. \usepackage{graphicx}
  9. \usepackage{listings}
  10. % package configuration
  11. \DeclareGraphicsExtensions{.pdf,.png,.jpg}
  12. \beamertemplatenavigationsymbolsempty
  13. % title page information
  14. \author{Julian Ospald}
  15. \institute{FH Bielefeld}
  16. \title{Haskell: introduction}
  17. % color definition
  18. \definecolor{solarized}{HTML}{002B36}
  19. \definecolor{mygreen}{rgb}{0,0.6,0}
  20. % macros and environments
  21. \newcommand{\code}[1]{\texttt{#1}}
  22. \newcommand{\setHaskellCodeStyle}{
  23. \lstset{
  24. language=Haskell,
  25. backgroundcolor=\color{lightgray},
  26. commentstyle=\color{mygreen},
  27. keywordstyle=\color{blue},
  28. frame=single,
  29. keepspaces=true
  30. }
  31. }
  32. \newcommand{\setCCodeStyle}{
  33. \lstset{
  34. language=C,
  35. backgroundcolor=\color{lightgray},
  36. commentstyle=\color{mygreen},
  37. keywordstyle=\color{blue},
  38. frame=single,
  39. keepspaces=true
  40. }
  41. }
  42. \newcommand{\setCppCodeStyle}{
  43. \lstset{
  44. language=C++,
  45. backgroundcolor=\color{lightgray},
  46. commentstyle=\color{mygreen},
  47. keywordstyle=\color{blue},
  48. frame=single,
  49. keepspaces=true
  50. }
  51. }
  52. \begin{document}
  53. \frame{\titlepage}
  54. \begin{frame}
  55. \frametitle{Why haskell?}
  56. A Haskeller might claim: haskell...
  57. \begin{itemize}[<+->]
  58. \item eliminates certain classes of bugs
  59. \item makes it easy to reason about code
  60. \item decreases the bus-factor
  61. \item makes it possible to apply huge changes to large programs without worrying about the implicit state machine
  62. \item makes it easier to program complex problems
  63. \item allows for clean APIs, even without any OOP
  64. \item a haskell program of 10K LOC isn't that much different to maintain as a 100K LOC
  65. \end{itemize}
  66. \vspace{\baselineskip}
  67. \onslide<+->
  68. We'll have to see if this holds true.
  69. \end{frame}
  70. \begin{frame}[fragile]
  71. \frametitle{Why haskell? (ctn.)}
  72. From C++ std:
  73. \setCppCodeStyle
  74. \begin{lstlisting}
  75. void pop();
  76. \end{lstlisting}
  77. \setCCodeStyle
  78. \onslide<+->
  79. From the C FLINT library:
  80. \begin{lstlisting}
  81. void fmpz_mod_poly_add(
  82. fmpz_mod_poly_t res,
  83. const fmpz_mod_poly_t poly1,
  84. const fmpz_mod_poly_t poly2);
  85. \end{lstlisting}
  86. \vspace{\baselineskip}
  87. \onslide<+->
  88. Regular C functions in real-world (omitting examples on purpose):
  89. \begin{itemize}[<+->]
  90. \item 100+ LOC
  91. \item at least 7 ifs, 4 whiles, 12 variables, 1 goto
  92. \item accesses both static and global variables
  93. \item indenting level of 5 or more
  94. \item a lot of memory management and custom-made error handling
  95. \item references everywhere!
  96. \end{itemize}
  97. \end{frame}
  98. \begin{frame}
  99. \frametitle{Why haskell? (ctn.)}
  100. You need to change only one single line in such a C function. You have to know:
  101. \begin{itemize}[<+->]
  102. \item does the order of function calls matter?
  103. \item how does the change effect the memory management? Do we have memory leaks? Do we access invalid memory?
  104. \item does it change the state of static or global variables?
  105. \item does it implicitly change the state of out-parameters?
  106. \item if it changes any of those states, is the function still correct?
  107. \item what happens if the program flow reaches this codepath with variable X in that particular state, while variable Z is NULL, and...
  108. \item did you just nuke a small former Soviet state?
  109. \end{itemize}
  110. \vspace{\baselineskip}
  111. \onslide<+->
  112. Conclusion: you really need to understand the complete environment of that line/function.
  113. \end{frame}
  114. \begin{frame}
  115. \frametitle{Why haskell? (ctn.)}
  116. But java helps! Does it?
  117. Sort of, because:
  118. \begin{itemize}[<+->]
  119. \item it improves APIs compared to C, since you can hide or encapsulate information in the state of an object
  120. \item it has a garbage collector, so you don't need to worry too much about memory
  121. \end{itemize}
  122. \onslide<+->
  123. Unfortunately, we:
  124. \begin{itemize}[<+->]
  125. \item now got even more states to keep track of (intellectual complexity?)
  126. \item have clouded the program flow... it's now about object-interaction with their explicit and implicit states
  127. \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
  128. \end{itemize}
  129. \onslide<+->
  130. 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"?
  131. \onslide<+->
  132. We are lucky. There is. It's called \textbf{Haskell}.
  133. \end{frame}
  134. \begin{frame}
  135. \frametitle{What is haskell?}
  136. Haskell is a \textbf{functional}, \textbf{pure}, \textbf{lazy} and \textbf{statically typed} high-level programming language.\\
  137. \onslide<+->
  138. \vspace{\baselineskip}
  139. 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?".
  140. \end{frame}
  141. \begin{frame}
  142. \frametitle{What does \textbf{functional} mean?}
  143. Think of haskell functions as regular \emph{mathematical} functions.
  144. \onslide<+->
  145. \vspace{\baselineskip}
  146. \includegraphics*[scale=0.4]{function-machine.png}
  147. \begin{itemize}[<+->]
  148. \item does this function write to the hard drive?
  149. \item does the output depend on anything else except the input (e.g. time, environment, ...)?
  150. \end{itemize}
  151. \onslide<+->
  152. \vspace{\baselineskip}
  153. It's all about \emph{input} and \emph{ouput} of functions! And that's it. Nothing else to worry about.
  154. \end{frame}
  155. \begin{frame}
  156. \frametitle{What does \textbf{functional} mean? (ctn.)}
  157. \begin{itemize}[<+->]
  158. \item \emph{first-class} citizens: functions are values and can be used as such
  159. \item a haskell program is what happens when \emph{expressions are evaluated}, it's not about executing instructions
  160. \end{itemize}
  161. \end{frame}
  162. \begin{frame}
  163. \frametitle{What does \textbf{pure} mean?}
  164. \emph{Referential transparency}, as in:
  165. \onslide<+->
  166. \begin{itemize}[<+->]
  167. \item everything (variables, data structures...) is \emph{immutable}
  168. \item expressions never have side-effects (remember: mathematical functions)
  169. \item same input $\mapsto$ same output... \emph{always}!
  170. \item replace a function with it's (return) value? Yes. (what happens in C or java if you do that?)
  171. \end{itemize}
  172. \onslide<+->
  173. \vspace{\baselineskip}
  174. possible benefits?
  175. \begin{itemize}[<+->]
  176. \item parallelism
  177. \item equational reasoning and refactoring
  178. \item less bugs!
  179. \end{itemize}
  180. \onslide<+->
  181. \vspace{\baselineskip}
  182. Question: call-by-value? call-by-reference? call-by-need?
  183. \end{frame}
  184. \begin{frame}
  185. \frametitle{What does \textbf{lazy} mean?}
  186. In haskell expressions are not evaluated until their results are actually needed. That has a lot of consequences, a few of them being:
  187. \onslide<+->
  188. \begin{itemize}[<+->]
  189. \item infinite data structures are now possible (recursive and non-recursive)
  190. \item defining new control structures by just defining a function (since not everything is evaluated... who needs if-then-else anyway?)
  191. \item important for compositional programming and efficiency
  192. \item laziness causes (memory) overhead, but most of the time the benefits outweigh the costs
  193. \end{itemize}
  194. \end{frame}
  195. \begin{frame}
  196. \frametitle{What does \textbf{statically typed} mean?}
  197. 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.\\
  198. \vspace{\baselineskip}
  199. 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).
  200. \end{frame}
  201. \begin{frame}
  202. \frametitle{What is haskell again?}
  203. Let's reiterate. Haskell is:
  204. \begin{itemize}
  205. \item functional
  206. \item pure
  207. \item lazy
  208. \item statically typed (and truly typesafe)
  209. \item even garbage collected
  210. \end{itemize}
  211. \end{frame}
  212. \begin{frame}
  213. \frametitle{Types, types, types}
  214. \begin{itemize}
  215. \item think in types!
  216. \item don't be afraid of type errors
  217. \item let the type-checker do the work for you (does this function do what I think it does?)
  218. \item understand functions just by looking at their type signature?
  219. \end{itemize}
  220. \end{frame}
  221. \begin{frame}[fragile]
  222. \frametitle{Declarations}
  223. Let's go!
  224. \setHaskellCodeStyle
  225. \begin{lstlisting}
  226. x :: Int
  227. x = 3
  228. -- how about this?
  229. x = 5
  230. -- Int vs Integer
  231. n :: Integer
  232. n = 12345678909876543219873409823349873498723498
  233. d :: Double
  234. d = 5.0
  235. c :: Char
  236. c = 'k'
  237. s :: String
  238. s = "Hello, world?"
  239. \end{lstlisting}
  240. \end{frame}
  241. \begin{frame}[fragile]
  242. \frametitle{Arithmetic and co.}
  243. \setHaskellCodeStyle
  244. GHCi:
  245. \begin{lstlisting}
  246. > 3 + 5
  247. > (3 :: Integer) + (5 :: Int)
  248. > 6 * 5.0
  249. > "Hello" ++ " world"
  250. > "Haskell" > "C++"
  251. > True && False
  252. \end{lstlisting}
  253. \end{frame}
  254. \begin{frame}[fragile]
  255. \frametitle{Functions and control structures}
  256. \setHaskellCodeStyle
  257. Let's make our first function. We want something like the following mathematical function\\
  258. $f(x) = x * x$\\
  259. \vspace{\baselineskip}
  260. How could the haskell code look like?
  261. \pause
  262. Almost the same:
  263. \begin{lstlisting}
  264. f x = x * x
  265. \end{lstlisting}
  266. \vspace{\baselineskip}
  267. GHCi...\\
  268. \pause
  269. \vspace{\baselineskip}
  270. What is a possible type signature for this function?
  271. \begin{lstlisting}
  272. f :: Int -> Int
  273. f x = x * x
  274. \end{lstlisting}
  275. 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.
  276. \end{frame}
  277. \begin{frame}[fragile]
  278. \frametitle{Functions and control structures (ctn.)}
  279. 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:
  280. \pause
  281. \setHaskellCodeStyle
  282. \vspace{\baselineskip}
  283. \begin{lstlisting}
  284. isZero :: Int -> Bool
  285. isZero 0 = True
  286. isZero x = False
  287. \end{lstlisting}
  288. \vspace{\baselineskip}
  289. 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.\\
  290. \vspace{\baselineskip}
  291. What might happen if we remove the second or the third line? What is a \textbf{partial function} and a \textbf{total function}?
  292. \end{frame}
  293. \begin{frame}[fragile]
  294. \frametitle{Functions and control structures (ctn.)}
  295. How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
  296. \pause
  297. \setHaskellCodeStyle
  298. \vspace{\baselineskip}
  299. \begin{lstlisting}
  300. mod2 :: Int -> Int
  301. mod2 x
  302. | x - 2 == 0 = 0
  303. | x - 2 < 0 = x
  304. | otherwise = mod2 (x - 2)
  305. \end{lstlisting}
  306. 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.
  307. \end{frame}
  308. \begin{frame}[fragile]
  309. \frametitle{Pairs/Tuples}
  310. Defining a pair is easy.
  311. \setHaskellCodeStyle
  312. \begin{lstlisting}
  313. p :: (Int, Char) -- this is the type
  314. p = (2, 'y') -- this is how we construct the pair
  315. -- pattern matching against pairs
  316. sumPair :: (Int, Int) -> Int
  317. sumPair (x, y) = x + y
  318. \end{lstlisting}
  319. \pause
  320. 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.
  321. \end{frame}
  322. \begin{frame}[fragile]
  323. \frametitle{Lists}
  324. \setHaskellCodeStyle
  325. 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.
  326. \pause
  327. We build lists by using either the \code{[]} notation:
  328. \begin{lstlisting}
  329. list1 :: [Integer]
  330. list1 = [1, 2]
  331. \end{lstlisting}
  332. \pause
  333. 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.
  334. \begin{lstlisting}
  335. list2 = 1 : []
  336. -- is this really a list?
  337. list3 = [1, 2] == 1 : 2 : []
  338. emptyList = []
  339. \end{lstlisting}
  340. \pause
  341. How about something more interesting:
  342. \begin{lstlisting}
  343. infiniteList = [1..]
  344. \end{lstlisting}
  345. \end{frame}
  346. \begin{frame}[fragile]
  347. \frametitle{Lists (ctn.)}
  348. Again, we can do pattern matching on lists.
  349. \setHaskellCodeStyle
  350. \begin{lstlisting}
  351. listLen :: [Integer] -> Integer
  352. listLen [] = 0
  353. listLen (x:xs) = 1 + listLen xs
  354. \end{lstlisting}
  355. We can also nest pattern matching:
  356. \begin{lstlisting}
  357. sumEveryTwo :: [Integer] -> [Integer]
  358. sumEveryTwo [] = 0
  359. sumEveryTwo (x:[]) = [x]
  360. sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
  361. \end{lstlisting}
  362. Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
  363. \end{frame}
  364. \begin{frame}
  365. \frametitle{Toolchain}
  366. You need:
  367. \begin{itemize}
  368. \item \textbf{GHC}: this is the Haskell compiler
  369. \item \textbf{GHCi}: this an interactive environment of GHC, similar to the interactive ruby shell \emph{irb}
  370. \item \textbf{The Haskell Platform}: a collection including GHC, GHCi and basic libraries
  371. \end{itemize}
  372. Go to \url{https://www.haskell.org/platform}\\
  373. For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}
  374. \end{frame}
  375. \begin{frame}
  376. \frametitle{Further reading and useful links}
  377. \begin{itemize}
  378. \item the most popular haskell course from Brent Yorgey:\\ \url{https://www.seas.upenn.edu/~cis194/fall14/spring13/lectures.html}
  379. \item very verbose and enthusiastic haskell book, good for reading once:\\ \url{http://learnyouahaskell.com}
  380. \item collection of websites teaching haskell:\\ \url{https://github.com/bitemyapp/learnhaskell}
  381. \item haskell programming tips (and wiki):\\ \url{https://wiki.haskell.org/Haskell_programming_tips}
  382. \item the standard module (similar to libc in C):\\ \url{https://hackage.haskell.org/package/base-4.7.0.0/docs/Prelude.html}
  383. \item debugging in haskell:\\ \url{https://wiki.haskell.org/Debugging}
  384. \end{itemize}
  385. \end{frame}
  386. \begin{frame}
  387. \frametitle{Sources}
  388. \begin{itemize}
  389. \item much content was borrowed or is based on the haskell course from Brent Yorgey:\\ \url{https://www.seas.upenn.edu/~cis194/fall14/spring13/lectures.html}
  390. \item a few small pieces from the LYAH book \url{http://learnyouahaskell.com}
  391. \item general information from wikipedia: \\ \url{https://en.wikipedia.org}
  392. \item general information from haskell wiki: \\ \url{https://wiki.haskell.org}
  393. \end{itemize}
  394. \end{frame}
  395. \end{document}