Commit 5c747a6b authored by Nicolas Lenz's avatar Nicolas Lenz
Browse files

Add second meeting

parent 14f355ef
Pipeline #950 passed with stages
in 22 seconds
......@@ -20,7 +20,7 @@ do_another_thing(x, y)
- In functional programming a program is no list of commands
- More a *collection of definitions*
```
```haskell
x = 4
x = 2
```
......
......@@ -8,7 +8,7 @@
## Basic Types in Haskell
- `Int`: Integer numbers (`-1`, `0`, `30`)
- `Boolean`: Truth values (`True`, `False`) (*capitalized!*)
- `Bool`: Truth values (`True`, `False`) (*capitalized!*)
- `Char`: Single characters (`'a'`, `'c'`)
- `String`: Text (`"Hello"`, `"Cheese sandwich"`)
- `a -> b`: Function with the
......
......@@ -25,9 +25,9 @@ double x = "calculate it yourself"
Pattern matching can "disassemble" values
```haskell
tail :: [Int] -> [Int]
tail [] = []
tail (x:xs) = xs
tail' :: [Int] -> [Int]
tail' [] = []
tail' (x:xs) = xs
```
......
......@@ -2,4 +2,11 @@
## Hoogle
https://hoogle.haskell.org/
Search for functions, types, typeclasses, modules, ... by name or type
- <https://hoogle.haskell.org/> (general)
- <https://www.stackage.org/> (for Stack)
**Example:** I want a function that returns the first element of a list of numbers. I don't know the name of the function.
But I can search for it by its type `[Int] -> Int`!
# Modules
## Modules
You found a function on Hoogle that is in the module `Data.List`?
```haskell
import Data.List
```
By default only modules from the `base` package are available though.
More about modules, packages and project management later!
# Simple IO
## Simple IO
How to make an executable/interactive program?
```haskell
main :: IO ()
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Hello " ++ name ++ "!")
```
# Higher Order Functions
## Examples
`map` & `filter`
# Types
## What is a type?
Let's recapture: Types classify and restrict values.
For example: The type of booleans `Bool`.
A Boolean is always either `True` or `False`.
## Sum Types
We see: a type can allow multiple values. These are called *sum types*. They are defined using `data` and `|`.
(Just accept `deriving Show` for now, it will be explained in the next chapter)
```haskell
data BaseColor = Red | Green | Blue deriving Show
v1 :: BaseColor
v1 = Green
v2 :: BaseColor
v2 = Blue
```
## Matching Sum Types
Pattern maching now works!
```haskell
isPartOfYellow :: BaseColor -> Bool
isPartOfYellow Red = True
isPartOfYellow Green = True
isPartOfYellow Blue = False
```
## Product Types
But what if values should carry other values (like *"attributes"*)?
Types can "include" other types. These are called *product types*. Sum and product types can be combined.
```haskell
data Degree = Bachelor | Master deriving Show
data Student = Student String String Degree deriving Show
data Device = Phone Int | Fridge String | Printer deriving Show
me :: Student
me = Student "Nicolas Lenz" "Computer Science" Master
notMe :: Student
notMe = Student "Horst-Dieter Müller" "Philosophy" Bachelor
myDevice :: Device
myDevice = Fridge "Bosch"
```
## Matching Product Types
Pattern matching also works here.
```haskell
isComputerScienceMaster :: Student -> Bool
isComputerScienceMaster (Student name "Computer Science" Master) = True
isComputerScienceMaster (Student name field degree) = False
```
## Type Variables
Previously:
```haskell
tail' :: [Int] -> [Int]
tail' [] = []
tail' (x:xs) = xs
```
Only works on `Int`. We can simply use a type variable!
```haskell
tail'' :: [a] -> [a]
tail'' [] = []
tail'' (x:xs) = xs
```
## Polymorphic Types
Type variables can also be used in types.
```haskell
data Rated a = Good a | Bad a deriving Show
rate :: Int -> Rated Int
rate 42 = Good 42
rate x = Bad x
```
## Recursive Types
Remember lists of type `a`, `[a]`?
A list is always either
- *an empty list* or
- *an element added in front of another list*.
## Making the List Type
```haskell
data List a = Empty | Cons a (List a) deriving Show
list :: List Int
list = Cons 1 (Cons 2 (Cons 3 Empty))
listToList :: [a] -> List a
listToList [] = Empty
listToList (x : xs) = Cons x (listToList xs)
```
# More Functions
## Comments
```haskell
x = 5 -- five
{-
multi-line comment!
-}
```
## where & let
`where` and `let` can be used for helper definitions.
```haskell
areaCircle :: Int -> Int
areaCircle r = p * (r ^ 2) where p = 3
areaCircle' :: Int -> Int
areaCircle' r = let p = 3 in p * (r ^ 2)
```
`where` is only a syntactic construct, `let .. in ..` is an expression itself and can be used pretty much everywhere (like `if .. then .. else ..`).
(Please use `pi` instead of `3`)
## Guards
Fancier if-alternative, like a switch
```haskell
rateNumber :: Int -> String
rateNumber x
| x < 42 = "meh"
| x == 42 = "cool"
| x < 100 = "okay"
| otherwise = "great"
```
## case
Pattern matching as expression
```haskell
isPartOfYellow :: BaseColor
isPartOfYellow Red = True
isPartOfYellow Green = True
isPartOfYellow Blue = False
isPartOfYellowCase :: BaseColor
isPartOfYellowCase x = case x of
Red -> True
Green -> True
Blue -> False
```
## Pattern Matching vs. Conditions
Note that function definitions and case definitions use pattern matching (e.g. can't use comparisons and functions, but disassemble values and assign parts to variables)
ifs and guards check boolean conditions (e.g. can use comparisons and functions, but not assign to variables)
## Wildcard
You can discard unwanted values in pattern matching with the wildcard `_`:
```haskell
isComputerScienceMaster :: Student -> Bool
isComputerScienceMaster (Student _ "Computer Science" Master) = True
isComputerScienceMaster (Student _ _ _) = False
```
Only usable in patterns, i.e. on *left-hand side* of the `=`!
## Infix
You can use backticks \` to write functions between their arguments.
## Example: QuickSort
Sort a `[Int]` using QuickSort!
- Take the first element of a list `p`
- Return QuickSort recursively on all elements smaller than `p`
- Then `p` itself
- Then the recursively sorted bigger elements
- An empty list is already sorted
## A Solution
```haskell
quicksort :: [Int] -> [Int]
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
```
# Typeclasses
## What are typeclasses?
Typeclasses in Haskell are a way of assigning "features" or "abilities" to types.
Comparable to *interfaces* in object-oriented programming or *traits* in Rust. Caution: they have nothing to do with object-oriented classes!
More concrete: Typeclasses ensure that types of that typeclass have certain functions available.
## Example
```haskell
class Ratable a where
rate :: a -> String
instance Ratable Int where
rate 42 = "Great"
rate _ = "Not so great"
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment