# Guards ## and Pattern Matching --- CS 135 // 2021-02-11 ## Administrivia - codePost Submissions + Only one member of your group needs to submit + Other member needs to click the "partner" link - Mathematics Honor Society ([Kappu Mu Epsilon](https://www.kappamuepsilon.org/about.php)) + Society for Drake students interested in Mathematics + [Spring 2021 Interest Survey](https://docs.google.com/forms/d/e/1FAIpQLSfvTpZ6V7zEqOfpl8ySn4PRDNFBI5CcRowpIBctLNpHTlY5VA/viewform) # Questions ## ...about anything? # Lab 1 Review # Lists ## Lists - Lists in Haskell are **linked lists** and use similar notation to Python ```haskell [True, True, False] -- Has type [Bool] ["Moi","Hei"] -- Has type [String] [] -- Has type [a] [[1,2],[3,4]] -- Has type [[Integer]] [1..7] -- Has type [Integer] ``` - Note that `String` is a synonym for `[Char]` + `'A'` defines a `Char` + `"abc"` is short for `['a', 'b', 'c']` ## List Operations ```haskell[1-2|4-5|7-8|10-11|13-14] -- returns the first element head :: [a] -> a -- returns everything except the first element tail :: [a] -> [a] -- returns everything except the last element init :: [a] -> [a] -- returns the n first elements take :: Int -> [a] -> [a] -- returns everything except the n first elements drop :: Int -> [a] -> [a] ``` ## List Operations ```haskell[1-2|4-5|7-8|10-11|13-14] -- lists are catenated with the ++ operator (++) :: [a] -> [a] -> [a] -- lists are indexed with the !! operator (!!) :: [a] -> Int -> a -- reverse a list reverse :: [a] -> [a] -- is this list empty? null :: [a] -> Bool -- the length of a list length :: [a] -> Int ``` ## List Exercise - Write a function that drops the first and last elements of the list ```haskell middle :: [a] -> [a] middle xs = init (tail xs) ``` # The `Maybe` Type ## Maybe - Many bugs are due to runtime exceptions like null pointers and division by zero - Haskell has a mechanism to avoid this issue entirely by including a `Maybe` type - `Maybe` has two constructors: + `Nothing` + `Just x` ## Maybe Examples - The `head` function is unsafe and creates a runtime exception if called on an **empty list** - Let's write a `safeHead` function that avoids this using a `Maybe` ```haskell safeHead :: [a] -> Maybe a safeHead [] = Nothing safeHead xs = Just (head xs) ``` ## Maybe Examples - What if we have two `Maybe Integer` values and we want to add them together? ```haskell addMaybe :: Maybe Integer -> Maybe Integer -> Maybe Integer addMaybe Nothing _ = Nothing addMaybe _ Nothing = Nothing addMaybe (Just x) (Just y) = Just (x + y) ``` # The `Either` Type ## Either - Another type similar to `Maybe` is called `Either` - An `Either` object could be one of two types - Commonly used as a replacement for `Maybe` if you need to pass along extra information when encountering a failure ```haskell safeDiv :: Integer -> Integer -> Either String Integer safeDiv _ 0 = Left "division by zero" safeDiv x y = Right (x `div` y) ``` # Self Checks ## Self Checks 1. How many values does `f x = [x,x]` return? 2. Why does the `Nothing 1` cause a type error? 3. What is the type of the function: ```haskell f x y = if x && y then Right x else Left "foo" ``` ## Self Checks 4. Which of the following functions could have the type `Bool -> Int -> [Bool]`? ```haskell f x y = [0, y] g x y = [x, True] h x y = [y, True] ``` 5. What is the type of this function? ```haskell justBoth a b = [Just a, Just b] ``` ## Lab Time - You will be sent into a breakout session with your partner for this week - Start working on the `Lab2a.hs` exercises - The lab is due before class on Tuesday + You only need to submit **once** on codePost and share the partner link with your partner