1. Javascript
2. Perl
3. Ruby
4. C++
5. Rust
6. Swift
7. Kotlin
8. Go
9. Scala
10. Julia
11. Lisp
12. Lua
13. C#
14. Prolog
15. Fortran
16. Cobol
17. Pascal
18. F#
# Lab 4a Debrief
# Questions
## ...about anything?
# The `$` Operator
## The `$` Operator
- You may notice the operator `$` used in Haskell code-bases, which is used to reduce the number of parentheses in code:
-
The following two expressions are equivalent:
```haskell
head (reverse "abcd")
head $ reverse "abcd"
```
-
Haskell is *left associative* by default, but the `$` is *right associative*, so it effectively evaluates the right-hand side first, even if there are no parentheses there
# Algebraic Datatypes
## Defining Our Own Types
- Thus far, we've used the **built-in types** of Haskell
- However, we know that defining our own types can yield a lot of power
## Algebraic Datatypes
- You can create an **algebraic datatype** using:
```haskell
data Coin = Heads | Tails
```
-
Now we can use it in functions using pattern matching:
```haskell
isHeads :: Coin -> Bool
isHeads Heads = True
isHeads Tails = False
```
## Derived Instances
- Haskell can automatically derive implementations of certain typeclasses
```haskell
data Coin = Heads | Tails deriving (Show, Eq)
```
-
`show Heads` produces the string `"Heads"`
-
`Tails == Tails` is true
## Algebraic Fields
- We can also add **fields** to datatypes
-
For example, it is common to represent a color in an RGB format with three integers
```haskell
data Color = RGB Int Int Int deriving (Show, Eq)
```
-
`RGB` is called a **constructor** for `Color` and takes three `Int`s as parameters
## Algebraic Fields
- We can now write functions with pattern matching:
```haskell
red :: Color -> Int
red (RGB r _ _) = r
green :: Color -> Int
green (RGB _ g _) = g
blue :: Color -> Int
blue (RGB _ _ b) = b
grayscale :: Color -> Color
grayscale (RGB r g b) = RGB avg avg avg
where avg = (r + g + b) `div` 3
```
## Record Syntax
- Creating functions like `red`, `green`, `blue` to extract out fields is so common, Haskell allows you to define types like this:
```haskell
data Color = RGB { red :: Int
, green :: Int
, blue :: Int }
deriving (Show, Eq)
```
-
This defines the same constructor `RGB`, but also defines the functions `red`, `green`, and `blue` to extract out the fields of the color
## Parameterized Types
- Sometimes it is helpful to define a **parameterized** type such as:
```haskell
data Maybe a = Nothing | Just a
```
-
We can also use multiple type parameters:
```haskell
data Either a b = Left a | Right b
```
## Recursive Types
- Haskell even lets you define **recursive** types such as:
```haskell
data List a = EmptyList | Cons a (List a)
```
-
We could even define a `Tree` data structure:
```haskell
data Tree a = EmptyTree | Node a (Tree a) (Tree a)
```
## Recursive Types
- Sometimes it is more helpful to define these with record syntax:
```haskell
data Tree a = EmptyTree
| Node { value :: a
, leftChild :: Tree a
, rightChild :: Tree a }
deriving (Show, Eq)
```
# Self Checks
### Check 1
Why can't we `map Nothing`?
1. Because `Nothing` doesn't take arguments
2. Because `Nothing` returns nothing
3. Because `Nothing` is a constructor.
### Check 2
- Suppose we define the following datatype
```haskell
data Boing = Frick String Boing (Int -> Bool)
```
what is the type of `Frick`?
---
```haskell
Boing -- 1
String -> Boing -> Int -> Bool -> Boing -- 2
String -> Boing -> (Int -> Bool) -> Boing -- 3
```
### Check 3
Suppose we define the following datatype
```haskell
data ThreeLists a b c = ThreeLists [a] [b] [c]
```
what is the type of the constructor `ThreeLists`?
---
```haskell
[a] -> [b] -> [c] -> ThreeLists -- 1
a -> b -> c -> ThreeLists a b c -- 2
[a] -> [b] -> [c] -> ThreeLists a b c -- 3
[a] -> [b] -> [c] -> ThreeLists [a] [b] [c] -- 4
```
### Check 4
If we define:
```haskell
data TwoLists a b = TwoList { aList :: [a]
, bList :: [b] }
```
what is the type of the function `aList`?
---
1. `aList` is not a function, it is a field
2. `TwoLists a b -> [a]`
3. `[a] -> TwoLists a b`
4. `[a]`
## Lab Time
- Please turn on your camera when in a breakout session if possible
-
Some students are hard of hearing and rely on lip-reading to follow along in conversation