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.
 
 
 

775 lines
25 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. % for \verb inside \item
  11. \usepackage[T1]{fontenc}
  12. \usepackage[Q=yes]{examplep}
  13. % package configuration
  14. \DeclareGraphicsExtensions{.pdf,.png,.jpg}
  15. \beamertemplatenavigationsymbolsempty
  16. % title page information
  17. \author{Julian Ospald}
  18. \institute{FH Bielefeld}
  19. \title{Haskell: introduction}
  20. % color definition
  21. \definecolor{solarized}{HTML}{002B36}
  22. \definecolor{mygreen}{rgb}{0,0.6,0}
  23. % macros and environments
  24. \newcommand{\code}[1]{\texttt{#1}}
  25. \newcommand{\setHaskellCodeStyle}{
  26. \lstset{
  27. language=Haskell,
  28. backgroundcolor=\color{lightgray},
  29. commentstyle=\color{mygreen},
  30. keywordstyle=\color{blue},
  31. frame=single,
  32. keepspaces=true
  33. }
  34. }
  35. \newcommand{\setCCodeStyle}{
  36. \lstset{
  37. language=C,
  38. backgroundcolor=\color{lightgray},
  39. commentstyle=\color{mygreen},
  40. keywordstyle=\color{blue},
  41. frame=single,
  42. keepspaces=true
  43. }
  44. }
  45. \newcommand{\setCppCodeStyle}{
  46. \lstset{
  47. language=C++,
  48. backgroundcolor=\color{lightgray},
  49. commentstyle=\color{mygreen},
  50. keywordstyle=\color{blue},
  51. frame=single,
  52. keepspaces=true
  53. }
  54. }
  55. \begin{document}
  56. \frame{\titlepage}
  57. \begin{frame}[allowframebreaks=0.8]
  58. \frametitle{Table of Contents}
  59. \tableofcontents
  60. \end{frame}
  61. \section{1. Why haskell?}
  62. \begin{frame}
  63. \frametitle{1. Why haskell?}
  64. A Haskeller might claim: haskell...
  65. \begin{itemize}[<+->]
  66. \item eliminates certain classes of bugs
  67. \item not only disallows bad programs, but also defines what constitutes a bad program
  68. \item makes it easy to reason about code
  69. \item decreases the bus-factor
  70. \item makes it possible to apply huge changes to large programs without worrying about the implicit state machine
  71. \item makes it easier to program complex problems
  72. \item allows for clean APIs, even without any OOP
  73. \item a haskell program of 10K LOC isn't that much different to maintain as a 100K LOC
  74. \item refactoring in haskell means abstraction
  75. \end{itemize}
  76. \vspace{\baselineskip}
  77. \onslide<+->
  78. We'll have to see if this holds true.
  79. \end{frame}
  80. \begin{frame}[fragile]
  81. \frametitle{1. Why haskell? (ctn.)}
  82. From C++ std:
  83. \setCppCodeStyle
  84. \begin{lstlisting}
  85. void pop();
  86. \end{lstlisting}
  87. \setCCodeStyle
  88. \onslide<+->
  89. From the C FLINT library:
  90. \begin{lstlisting}
  91. void fmpz_mod_poly_add(
  92. fmpz_mod_poly_t res,
  93. const fmpz_mod_poly_t poly1,
  94. const fmpz_mod_poly_t poly2);
  95. \end{lstlisting}
  96. \vspace{\baselineskip}
  97. \onslide<+->
  98. Regular C functions in real-world (omitting examples on purpose):
  99. \begin{itemize}[<+->]
  100. \item 100+ LOC
  101. \item at least 7 ifs, 4 whiles, 12 variables, 1 goto
  102. \item accesses both static and global variables
  103. \item indenting level of 5 or more
  104. \item a lot of memory management and custom-made error handling
  105. \item references everywhere!
  106. \end{itemize}
  107. \end{frame}
  108. \begin{frame}
  109. \frametitle{1. Why haskell? (ctn.)}
  110. You need to change only one single line in such a C function. You have to know:
  111. \begin{itemize}[<+->]
  112. \item does the order of function calls matter?
  113. \item how does the change effect the memory management? Do we have memory leaks? Do we access invalid memory?
  114. \item does it change the state of static or global variables?
  115. \item does it implicitly change the state of out-parameters?
  116. \item if it changes any of those states, is the function still correct?
  117. \item what happens if the program flow reaches this code-path with variable X in that particular state, while variable Z is NULL, and...
  118. \item did you just nuke a small former Soviet state?
  119. \end{itemize}
  120. \vspace{\baselineskip}
  121. \onslide<+->
  122. Conclusion: you really need to understand the complete environment of that line/function.
  123. \end{frame}
  124. \begin{frame}
  125. \frametitle{1. Why haskell? (ctn.)}
  126. But java helps! Does it?
  127. Sort of, because:
  128. \begin{itemize}[<+->]
  129. \item it improves APIs compared to C, since you can hide or encapsulate information in the state of an object
  130. \item it has a garbage collector, so you don't need to worry too much about memory
  131. \item an experienced programmer will find it easier to manage side-effects in Java, because you can e.g. have every impure functions throw an IO exception
  132. \end{itemize}
  133. \end{frame}
  134. \begin{frame}
  135. \frametitle{1. Why haskell? (ctn.)}
  136. Unfortunately, with java we:
  137. \begin{itemize}[<+->]
  138. \item now got even more states to keep track of (intellectual complexity?)
  139. \item have clouded the program flow... it's now about object-interaction with their explicit and implicit states and because of the increase of indirection, it might get even harder to do actual abstraction
  140. \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 missile
  141. \end{itemize}
  142. \onslide<+->
  143. 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"?
  144. \onslide<+->
  145. \\
  146. \vspace{\baselineskip}
  147. We are lucky. There is. It's called \textbf{Haskell}.
  148. \end{frame}
  149. \section{2. What is haskell?}
  150. \begin{frame}
  151. \frametitle{2. What is haskell?}
  152. Haskell is a \textbf{functional}, \textbf{pure}, \textbf{lazy} and \textbf{statically typed} high-level programming language.\\
  153. \onslide<+->
  154. \vspace{\baselineskip}
  155. 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?".
  156. \end{frame}
  157. \subsection{2.1. What does functional mean?}
  158. \begin{frame}
  159. \frametitle{2.1. What does \textbf{functional} mean?}
  160. Think of haskell functions as regular \emph{mathematical} functions.
  161. \onslide<+->
  162. \vspace{\baselineskip}
  163. \includegraphics*[scale=0.4]{function-machine.png}
  164. \begin{itemize}[<+->]
  165. \item does this function write to the hard drive?
  166. \item does the output depend on anything else except the input (e.g. time, environment, ...)?
  167. \end{itemize}
  168. \onslide<+->
  169. \vspace{\baselineskip}
  170. It's all about \emph{input} and \emph{output} of functions! And that's it. Nothing else to worry about.
  171. \end{frame}
  172. \begin{frame}
  173. \frametitle{2.1. What does \textbf{functional} mean? (ctn.)}
  174. \begin{itemize}[<+->]
  175. \item \emph{first-class} citizens: functions are values and can be used as such
  176. \item a haskell program is what happens when \emph{expressions are evaluated}, it's not about executing instructions
  177. \end{itemize}
  178. \end{frame}
  179. \subsection{2.2. What does pure mean?}
  180. \begin{frame}
  181. \frametitle{2.2. What does \textbf{pure} mean?}
  182. \emph{Referential transparency}, as in:
  183. \onslide<+->
  184. \begin{itemize}[<+->]
  185. \item everything (variables, data structures...) is \emph{immutable}
  186. \item expressions never have side-effects (remember: mathematical functions)
  187. \item same input $\mapsto$ same output... \emph{always}!
  188. \item replace a function with it's (return) value? Yes. What happens in C or java if you do that? Remember \code{void pop();}?
  189. \end{itemize}
  190. \onslide<+->
  191. \vspace{\baselineskip}
  192. possible benefits?
  193. \begin{itemize}[<+->]
  194. \item parallelism
  195. \item equational reasoning and refactoring
  196. \item less bugs!
  197. \end{itemize}
  198. \onslide<+->
  199. \vspace{\baselineskip}
  200. Question: call-by-value? call-by-reference? call-by-need?
  201. \end{frame}
  202. \subsection{2.3. What does lazy mean?}
  203. \begin{frame}
  204. \frametitle{2.3. What does \textbf{lazy} mean?}
  205. In haskell expressions are not evaluated until their results are actually needed. That has a lot of consequences, a few of them being:
  206. \onslide<+->
  207. \begin{itemize}[<+->]
  208. \item infinite data structures are now possible (recursive and non-recursive)
  209. \item defining new control structures by just defining a function (since not everything is evaluated... who needs if-then-else anyway?)
  210. \item important for compositional programming and efficiency
  211. \end{itemize}
  212. \end{frame}
  213. \subsection{2.4. What does statically typed mean?}
  214. \begin{frame}
  215. \frametitle{2.4. What does \textbf{statically typed} mean?}
  216. 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.\\
  217. \vspace{\baselineskip}
  218. It is possible to simulate dynamic types however, i.e. types which may be converted to the type you need them in, following rigid rules. In the end, they are still statically type-checked (as in: the compiler always knows what's going on).
  219. \end{frame}
  220. \subsection{2.5. Summary}
  221. \begin{frame}
  222. \frametitle{2.5. What is haskell again?}
  223. Let's reiterate. Haskell is:
  224. \begin{itemize}
  225. \item functional
  226. \item pure
  227. \item lazy
  228. \item statically typed (and truly type-safe)
  229. \item even garbage collected
  230. \item the world's finest imperative language (what??)
  231. \end{itemize}
  232. \end{frame}
  233. \section{3. How to think haskell?}
  234. \begin{frame}
  235. \frametitle{3. How to think haskell?}
  236. \begin{itemize}[<+->]
  237. \item forget about imperative, procedural, OOP; forget about any programming language
  238. \item think in types!
  239. \begin{itemize}[<+->]
  240. \item don't be afraid of type errors
  241. \item let the type-checker do the work for you (does this function do what I think it does?)
  242. \item understand functions just by looking at their type signature?
  243. \end{itemize}
  244. \item think abstract!
  245. \begin{itemize}[<+->]
  246. \item don't repeat yourself!
  247. \item "develop a solution space, rather than an individual solution" -- Ralf Hinze
  248. \item "imagine a graph, rather than a single path" -- Ralf Hinze
  249. \item "first solve a more general problem, then extract the interesting bits and pieces by transforming the general program into more specialised ones" -- Ralf Hinze
  250. \end{itemize}
  251. \item solve atomic parts of general problems and combine them into greater solutions
  252. \end{itemize}
  253. \end{frame}
  254. \section{4. Declarations}
  255. \begin{frame}[fragile]
  256. \frametitle{4. Declarations}
  257. Let's go!
  258. \setHaskellCodeStyle
  259. \begin{lstlisting}
  260. x :: Int
  261. x = 3
  262. -- how about this?
  263. x = 5
  264. -- Int vs Integer
  265. n :: Integer
  266. n = 12345678909876543219873409823349873498723498
  267. d :: Double
  268. d = 5.0
  269. c :: Char
  270. c = 'k'
  271. s :: String
  272. s = "Hello, world?"
  273. \end{lstlisting}
  274. \end{frame}
  275. \section{5. Arithmetic and co.}
  276. \begin{frame}[fragile]
  277. \frametitle{5. Arithmetic and co.}
  278. \setHaskellCodeStyle
  279. GHCi:
  280. \begin{lstlisting}
  281. > 3 + 5
  282. > (3 :: Integer) + (5 :: Int)
  283. > 6 * 5.0
  284. > "Hello" ++ " world"
  285. > "Haskell" > "C++"
  286. > True && False
  287. \end{lstlisting}
  288. \end{frame}
  289. \section{6. Functions and control structures}
  290. \begin{frame}[fragile]
  291. \frametitle{6. Functions and control structures}
  292. \setHaskellCodeStyle
  293. Let's make our first function. We want something like the following mathematical function\\
  294. $f(x) = x * x$\\
  295. \vspace{\baselineskip}
  296. How could the haskell code look like?
  297. \pause
  298. Almost the same:
  299. \begin{lstlisting}
  300. f x = x * x
  301. \end{lstlisting}
  302. \vspace{\baselineskip}
  303. GHCi...\\
  304. \pause
  305. \vspace{\baselineskip}
  306. What is a possible type signature for this function?
  307. \begin{lstlisting}
  308. f :: Int -> Int
  309. f x = x * x
  310. \end{lstlisting}
  311. 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.
  312. \end{frame}
  313. \begin{frame}[fragile]
  314. \frametitle{6. Functions and control structures (ctn.)}
  315. 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 arguments. Let's see:
  316. \pause
  317. \setHaskellCodeStyle
  318. \vspace{\baselineskip}
  319. \begin{lstlisting}
  320. isZero :: Int -> Bool
  321. isZero 0 = True
  322. isZero x = False
  323. \end{lstlisting}
  324. \vspace{\baselineskip}
  325. 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.\\
  326. \vspace{\baselineskip}
  327. What might happen if we remove the second or the third line? What is a \textbf{partial function} and a \textbf{total function}?
  328. \end{frame}
  329. \begin{frame}[fragile]
  330. \frametitle{6. Functions and control structures (ctn.)}
  331. How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
  332. \pause
  333. \setHaskellCodeStyle
  334. \vspace{\baselineskip}
  335. \begin{lstlisting}
  336. mod2 :: Int -> Int
  337. mod2 x
  338. | x - 2 == 0 = 0
  339. | x - 2 < 0 = x
  340. | otherwise = mod2 (x - 2)
  341. \end{lstlisting}
  342. 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 easier to read and catch all other cases of input.
  343. \end{frame}
  344. \section{7. Pairs/Tuples}
  345. \begin{frame}[fragile]
  346. \frametitle{7. Pairs/Tuples}
  347. Defining a pair is easy.
  348. \setHaskellCodeStyle
  349. \begin{lstlisting}
  350. p :: (Int, Char) -- this is the type
  351. p = (2, 'y') -- this is how we construct the pair
  352. -- pattern matching against pairs
  353. sumPair :: (Int, Int) -> Int
  354. sumPair (x, y) = x + y
  355. \end{lstlisting}
  356. \pause
  357. 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.
  358. \end{frame}
  359. \section{8. Lists}
  360. \begin{frame}[fragile]
  361. \frametitle{8. Lists}
  362. \setHaskellCodeStyle
  363. 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.
  364. \pause
  365. We build lists by using either the \code{[]} notation:
  366. \begin{lstlisting}
  367. list1 :: [Integer]
  368. list1 = [1, 2]
  369. \end{lstlisting}
  370. \pause
  371. 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.
  372. \begin{lstlisting}
  373. emptyList = []
  374. list2 = 1 : []
  375. -- is this really a list?
  376. list3 = [1, 2] == 1 : 2 : []
  377. \end{lstlisting}
  378. \pause
  379. How about something more interesting:
  380. \begin{lstlisting}
  381. infiniteList = [1..]
  382. \end{lstlisting}
  383. \end{frame}
  384. \begin{frame}[fragile]
  385. \frametitle{8. Lists (ctn.)}
  386. Let's check on a few very common list operations:
  387. \setHaskellCodeStyle
  388. \begin{lstlisting}
  389. > [1, 2] ++ [4, 5] -- append two lists
  390. > head [1, 2, 3] -- first element
  391. > tail [1, 2, 3] -- everything after the head
  392. > reverse [1, 2, 3] -- reverse a list
  393. > take 2 [1, 2, 3] -- take the first two elements
  394. > drop 2 [1, 2, 3] -- drop the first two elements
  395. > sum [1, 2, 3]
  396. > elem 7 [1, 2, 3] -- is there a 7 in the list?
  397. \end{lstlisting}
  398. \pause
  399. A String in haskell is just a list of Chars!
  400. \setHaskellCodeStyle
  401. \begin{lstlisting}
  402. > ['a', 'b', 'c']
  403. > 'a' : []
  404. > head "abc"
  405. > 'a' ++ 'c'
  406. \end{lstlisting}
  407. \end{frame}
  408. \begin{frame}[fragile]
  409. \frametitle{8. Lists (ctn.)}
  410. Again, we can do pattern matching on lists.
  411. \setHaskellCodeStyle
  412. \begin{lstlisting}
  413. listLen :: [Integer] -> Integer
  414. listLen [] = 0
  415. listLen (x:xs) = 1 + listLen xs
  416. \end{lstlisting}
  417. \pause
  418. We can also nest pattern matching:
  419. \begin{lstlisting}
  420. sumEveryTwo :: [Integer] -> [Integer]
  421. sumEveryTwo [] = 0
  422. sumEveryTwo (x:[]) = [x]
  423. sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
  424. \end{lstlisting}
  425. Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
  426. \end{frame}
  427. \begin{frame}[fragile]
  428. \frametitle{8. Lists (ctn.)}
  429. Haskell also supports \textbf{list comprehension} which is basically syntactic sugar for what we already know from maths.\\
  430. Let's define a set that contains the first ten even natural numbers:\\
  431. \pause
  432. $S = \{2 \times x\ |\ x \in \mathbb{N},\ x \leq 10\}$\\
  433. \vspace{\baselineskip}
  434. \pause
  435. How does this look in haskell?
  436. \pause
  437. \setHaskellCodeStyle
  438. \begin{lstlisting}
  439. > [x*2 | x <- [1..10]]
  440. \end{lstlisting}
  441. \pause
  442. Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:
  443. \pause
  444. \setHaskellCodeStyle
  445. \begin{lstlisting}
  446. > [x | x <- [50..100], mod x 12 == 0]
  447. \end{lstlisting}
  448. \code{x <- [50..100]} is the binding, while \code{mod x 12 == 0} is the predicate, separated by a comma. We can have multiple predicates.
  449. \end{frame}
  450. \section{9. Algebraic Data Types}
  451. \begin{frame}[fragile]
  452. \frametitle{9. Algebraic Data Types}
  453. Of course we can also define our own data types in haskell. One very common type is the \emph{enumeration}. For example, we could define a data type for the Week:
  454. \setHaskellCodeStyle
  455. \begin{lstlisting}
  456. data WeekDay = Monday
  457. | Tuesday
  458. | Thursday
  459. | Wednesday
  460. | Friday
  461. | Saturday
  462. | Sunday
  463. \end{lstlisting}
  464. This declares the new data type \code{WeekDay} with 7 \emph{constructors}. That means \code{Monday}, \code{Tuesday} etc. are all values of the type \code{WeekDay}.
  465. \pause
  466. \\
  467. We could now define a whole week, by creating a list:
  468. \pause
  469. \setHaskellCodeStyle
  470. \begin{lstlisting}
  471. week :: [WeekDay]
  472. week = [Monday, Tuesday, Thursday, Wednesday
  473. , Friday, Saturday, Sunday]
  474. \end{lstlisting}
  475. \end{frame}
  476. \begin{frame}[fragile]
  477. \frametitle{9. Algebraic Data Types (ctn.)}
  478. And we can again \emph{pattern match} on our \code{WeekDay} type. Let's find out if a given day is a Monday:
  479. \pause
  480. \setHaskellCodeStyle
  481. \begin{lstlisting}
  482. isMonday :: WeekDay -> Bool
  483. isMonday Monday = True
  484. isMonday x = False
  485. \end{lstlisting}
  486. \end{frame}
  487. \begin{frame}[fragile]
  488. \frametitle{9. Algebraic Data Types (ctn.)}
  489. But we can do more than enumerations. How about we do some error handling? Let's say we want a function to return an \code{Int}, but in case something went horribly wrong, we don't just want to return a 0 or some magic number, but a proper error message. Here we go:
  490. \pause
  491. \setHaskellCodeStyle
  492. \begin{lstlisting}
  493. data MaybeInt = NoError Int
  494. | Error String
  495. \end{lstlisting}
  496. \pause
  497. So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types:
  498. \setHaskellCodeStyle
  499. \begin{lstlisting}
  500. > :t NoError
  501. NoError :: Int -> MaybeInt
  502. > :t Error
  503. Error :: String -> MaybeInt
  504. \end{lstlisting}
  505. \pause
  506. And now we can do sanity checks:
  507. \setHaskellCodeStyle
  508. \begin{lstlisting}
  509. calcSomething :: Int -> MaybeInt
  510. calcSomething x
  511. | x < 100 = NoError (x * 5)
  512. | otherwise = Error "Int out of range!"
  513. \end{lstlisting}
  514. \end{frame}
  515. \begin{frame}[fragile]
  516. \frametitle{9. Algebraic Data Types (ctn.)}
  517. And pattern match on it as well:
  518. \setHaskellCodeStyle
  519. \begin{lstlisting}
  520. addIntToList :: MaybeInt -> [Int]
  521. addIntToList (NoError x) = [x]
  522. addIntToList (Error str) = []
  523. \end{lstlisting}
  524. So if we got an error, we just return an empty list, otherwise we return a list with the \code{Int} as its only element.
  525. \end{frame}
  526. \begin{frame}[fragile]
  527. \frametitle{9. Algebraic Data Types (ctn.)}
  528. Let's define something more complex. How about a tree?
  529. \pause
  530. \setHaskellCodeStyle
  531. \begin{lstlisting}
  532. data Tree = Leaf Char
  533. | Node Tree Int Tree
  534. \end{lstlisting}
  535. Uh... that looks mean. Let's examine this.\\
  536. \pause
  537. We have:
  538. \begin{itemize}[<+->]
  539. \item defined a data type \code{Tree}
  540. \item a constructor \code{Leaf} of type \code{Tree} with one arguments of type \code{Char}
  541. \item a constructor \code{Node} of type \code{Tree} with 3 arguments
  542. \begin{itemize}[<+->]
  543. \item \code{Tree}
  544. \item \code{Int}
  545. \item \code{Tree}
  546. \end{itemize}
  547. \end{itemize}
  548. \onslide<+->
  549. That means: a \code{Tree} can either be a \code{Leaf} or an internal \code{Node} with two sub-trees. If we want to create a \code{Leaf}, we have to pass the constructor a \code{Char}. If we want to create a \code{Node}, we have to pass 3 arguments, in order: another \code{Tree}, an \code{Int} and yet another \code{Tree}.\\
  550. So we can save information in the leafs (\code{Char}) and in the internal nodes (\code{Int}).\\
  551. This is just an example. There are endless more ways of trees.
  552. \end{frame}
  553. \begin{frame}[fragile]
  554. \frametitle{9. Algebraic Data Types (ctn.)}
  555. Let's build our tree:
  556. \setHaskellCodeStyle
  557. \begin{lstlisting}
  558. tree :: Tree
  559. tree = Node
  560. (Leaf 'x')
  561. 1
  562. (Node
  563. (Leaf 'y')
  564. 2
  565. (Leaf 'z')
  566. )
  567. \end{lstlisting}
  568. See board...
  569. \end{frame}
  570. \begin{frame}[fragile]
  571. \frametitle{9. Algebraic Data Types (ctn.)}
  572. So if we want to generalize it, an algebraic data type has one or more \textbf{constructors}, and each of them can have zero or more arguments. E.g.:
  573. \setHaskellCodeStyle
  574. \begin{lstlisting}
  575. data AlgDataType = Constr1 Type11 Type12
  576. | Constr2 Type21
  577. | Constr3 Type31 Type32 Type33
  578. | Constr4
  579. \end{lstlisting}
  580. \end{frame}
  581. \section{10. Questions so far?}
  582. \begin{frame}
  583. \frametitle{10. Questions so far?}
  584. \begin{itemize}
  585. \item How do functions with multiple parameters look like?
  586. \item ...
  587. \end{itemize}
  588. \end{frame}
  589. \section{11. Common misconceptions}
  590. \begin{frame}
  591. \frametitle{11. Common misconceptions}
  592. Now that we know the basics, let's clear up some common misconceptions about haskell.
  593. \begin{itemize}[<+->]
  594. \item haskell is only a language for university professors
  595. \item haskell is not used in real world, see \url{https://wiki.haskell.org/Haskell_in_industry}
  596. \begin{itemize}
  597. \item Microsoft
  598. \item NVIDIA
  599. \item facebook
  600. \item Google
  601. \item Intel
  602. \item AT\Q{&}T
  603. \end{itemize}
  604. \item you cannot model states in haskell
  605. \item you cannot write larger programs in haskell
  606. \item you cannot write useful programs in haskell
  607. \item you cannot implement imperative algorithms
  608. \item you cannot do concurrency, cryptography, web development, ... in haskell
  609. \end{itemize}
  610. \onslide<+->
  611. You can!
  612. \end{frame}
  613. \section{12. Difficulties}
  614. \begin{frame}
  615. \frametitle{12. Difficulties}
  616. Haskell is very powerful and can be used for pretty much anything. However, there are difficulties in any language. Let's name a few for haskell:
  617. \begin{itemize}[<+->]
  618. \item intellectual complexity? New way of thinking?
  619. \item although you rarely need it in haskell, debugging can be difficult at times
  620. \item because the type system is extremely powerful/complex, type error messages can be very confusing and don't always show the error you expected
  621. \item no premium-like IDE with every possible feature (yet)
  622. \item dynamic linking is sort of WIP yet, lots of ABI breakage
  623. \item because most of the world thinks in imperative style languages, it's often difficult to find pseudo-code for functional style languages, so you end up reverse-engineering algorithms
  624. \item some problems that are trivial in imperative languages, can be very difficult to solve in idiomatic haskell and vice versa
  625. \item practical cryptography is possible, but a difficult topic in haskell, see \url{https://mail.haskell.org/pipermail/haskell-cafe/2015-February/118059.html}
  626. \end{itemize}
  627. \end{frame}
  628. \section{13. Toolchain}
  629. \begin{frame}
  630. \frametitle{13. Toolchain}
  631. How to get started? You need:
  632. \begin{itemize}
  633. \item \textbf{GHC}: this is the Haskell compiler
  634. \item \textbf{GHCi}: this an interactive environment of GHC, similar to the interactive ruby shell \emph{irb}
  635. \item \textbf{The Haskell Platform}: a collection including GHC, GHCi and basic libraries
  636. \end{itemize}
  637. Go to \url{https://www.haskell.org/platform}\\
  638. For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}
  639. \end{frame}
  640. \section{14. What you should know}
  641. \begin{frame}
  642. \frametitle{14. What you should know now}
  643. \begin{itemize}
  644. \item what haskell is
  645. \item how you write haskell functions
  646. \item how you handle lists and pairs
  647. \item how you do pattern matching
  648. \item how you create your own data types
  649. \end{itemize}
  650. \end{frame}
  651. \section{15. Questions for you}
  652. \begin{frame}[fragile]
  653. \frametitle{15. Questions for you}
  654. \begin{itemize}
  655. \item What are side effects?
  656. \item What is referential transparency?
  657. \item Can you have referential transparency with side effects?
  658. \item What does the output of a haskell function depend on?
  659. \item What is laziness?
  660. \item When are types checked in haskell?
  661. \end{itemize}
  662. \vspace{\baselineskip}
  663. Does this compile? If not, fix it. Is this a total function?
  664. \setHaskellCodeStyle
  665. \begin{lstlisting}
  666. data IntOrDouble = MkDouble Double
  667. | MkInt Int
  668. f :: Int -> IntOrDouble
  669. f 0 = 0.5
  670. \end{lstlisting}
  671. \end{frame}
  672. \section{16. Links}
  673. \begin{frame}
  674. \frametitle{16. Further reading and useful links}
  675. \begin{itemize}
  676. \item the most popular haskell course from Brent Yorgey:\\ \url{https://www.seas.upenn.edu/~cis194/fall14/spring13/lectures.html}
  677. \item very verbose and enthusiastic haskell book, good for reading once:\\ \url{http://learnyouahaskell.com}
  678. \item collection of websites teaching haskell:\\ \url{https://github.com/bitemyapp/learnhaskell}
  679. \item haskell programming tips (and wiki):\\ \url{https://wiki.haskell.org/Haskell_programming_tips}
  680. \item the standard module (similar to libc in C):\\ \url{https://hackage.haskell.org/package/base-4.7.0.0/docs/Prelude.html}
  681. \item debugging in haskell:\\ \url{https://wiki.haskell.org/Debugging}
  682. \end{itemize}
  683. \end{frame}
  684. \section{17. Sources}
  685. \begin{frame}
  686. \frametitle{17. Sources}
  687. \begin{itemize}
  688. \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}
  689. \item a few small pieces from the LYAH book \url{http://learnyouahaskell.com}
  690. \item general information from wikipedia: \\ \url{https://en.wikipedia.org}
  691. \item general information from haskell wiki: \\ \url{https://wiki.haskell.org}
  692. \end{itemize}
  693. \end{frame}
  694. \end{document}