---
# System prepended metadata

title: Untitled

---

## PPT Oct 13

---

## Last week's exercise
How was it? Hard? Easy?

---

## Tricky parts

---

### maxOf3
```haskell=
maxOf3 :: Int -> Int -> Int -> Int
maxOf3 x y z
    = maxOf2 x (maxOf2 y z)
```

---

## Common Mistakes

---

### Align Equal signs

No:
```haskell=
foo x y
  | x == y = "bar"
  | otherwise = "baz"
```

Yes:
```haskell=
foo x y
  | x == y    = "bar"
  | otherwise = "baz"
```

---

### You can compare characters instead of converting them to Int
Such as:
```haskell=
isADigit :: Char -> Bool
isADigit c
  = c >= '0' && c <= '9'
```

Instead of:
```haskell=
isADigit :: Char -> Bool
isADigit c
  = ord c >= ord '0' && ord c <= ord '9'
```

---


### Magic numbers
```haskell=
digitToInt :: Char -> Int
digitToInt c
  | isADigit c = ord c - 48
```

or even

```haskell=
digitToInt :: Char -> Int
digitToInt c
  | isADigit c = ord c - 0x30
```

---

### Function calls are without brackets
```haskell=
...
  = foo (bar)
```
Only use brackets where necessary OR when it helps readability

---

### -10000

```haskell=
foo
  = if <expression> then True else False
```

should be:
```haskell=
foo
  = <expression>
```

---

### Same for guards

```haskell=
foo
  | <expression> = True
  | otherwise    = False
```

should be:
```haskell=
foo
  = <expression>
```

---

## Marking
* 6.5 base score for correct implementation
* 5.5 base score for almost correct solution (e.g. a function is incorrect, not following the spec)
* etc...
* extra points for extra useful tests, extensions, valuable comments, especially elegant solution
* minus points for mistakes and errors
* There are no extra points for extra tests if the solution itself is not correct
* I always round up

---

## Exercises

Kon or Tony??? Tony

```haskell=
pos :: [Int] -> Int -> Int
pos l target
  = 
```

```haskell Alex's Code
pos :: [Int] -> Int -> Int
pos l target
  -- Iterates through the list till the list element is equal to target
  | length l  = -1
  | otherwise = parse target 0
  where
  
      parse :: Int -> Int -> Int
      parse var i
      -- Needs to pop head after checking
          = i if (head l == var) else parse target (i + 1)
```

GERARD'S STUFF, BACK OFF

```haskell=
-- This works somehow
pos :: [Int] -> Int -> Int
pos l target
  = pos' l target 1
  where
    pos' [] target n
      = error "Not in list"
    pos' l target n
      | head l == target = n
      | otherwise        = pos' (tail l) target (n + 1)
```

GERARD'S STUFF OVER

## Bartek's solution

Bartek's solution under
|| || || || || ||
\/ \/ \/ \/ \/ \/
```haskell=
pos :: [Int] -> Int -> Int
pos l target
  | head l == target = 0
  | otherwise        = succ (pos (tail l) target)

pos [] target = 0
```

## Otto's solution

```
[1, 2, 3] == 1 : 2 : 3 : []
```

```lisp
;; in Common Lisp
(cons 1 (cons 2 (cons 3 nil)))
```

```haskell=
pos' (x : xs) target accum
  | x == target = accum
  | otherwise   = pos' xs target $ succ accum
  
pos' [] target accum = error "List empty or element not found"
 pos list target = pos' list target 0
  
```



## Oli's One
```haskell=
pos :: [Int] -> Int -> Int
pos list target
    = pos' 0
    where pos' index
        | atEnd     = error
        | isMatch   = index
        | otherwise = pos' $ succ index
        where posChar = list !! index
              isMatch = posChar == target
              atEnd   = index >= length list
```

## Jordan Hall
````haskell=
pos :: [Int] -> Int -> Int
pos l target
  | length l > 0  = getIndex l target 0
  | otherwise     = -1
  
  where 
    getIndex l target index
      | index >= length l     = -1
      | l !! index == target  = index
      | otherwise             = getIndex l target (index + 1)
````


## Emil Filip's solution
```haskell=
-- Recursively iterates over a given list until it finds the first occurrence 
-- of a given integer in the list. Returns the index of the first occurrence.
pos :: [Int] -> Int -> Int
pos l target
  | len == 0  = (-1)
  | otherwise = pos' l target 0
  where
    len = length l
    pos' :: [Int] -> Int -> Int ->Int
    pos' l t i
      | i >= len  = (-1)
      | l!!i == t = i
      | otherwise = pos' l t (i + 1)
```








## Panayiotis Gavriil
```haskell=
pos :: [Int] -> Int -> Int
pos l target
  = [a | a <- [0..(length l - 1)], l !! a == target] !! 0
```




## Fibonacci sequence

```haskell=
-- T_{n + 2} = T_{n+1} + T_{n}
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib 2 = 1
fib n 
  | n < 0     = error "n > 0"
  | otherwise = fib (n - 1) + fib (n - 2)

gr :: Floating a => Int -> a
gr n = fromIntegral (fib n) / fromIntegral (fib (n - 1))

gr' :: Floating a => a -> Int -> a
gr' errorRate n
  | abs (phi - phi') < errorRate = phi
  | otherwise                    = gr' errorRate $ succ n
  where
    phi  = gr n
    phi' = gr $ succ n
    
gr' :: Floating a => a -> a
goldenRatio errorRate = gr' errorRate 2

-- *Main> goldenRatio 0.0001
-- 1.6179775280898876
```