Part 18

Quiz and cheatsheet

What is the type of swap . swap?

  1. (a, b) -> (a, b)
  2. (a, b) -> (b, a)
  3. a -> a

What is the type of \f g x -> (f x, g x)?

  1. (a -> b) -> (c -> d) -> (a,c) -> (b, d)
  2. (a -> b) -> (a -> c) -> a -> (b, c)
  3. (a -> b) -> (b -> a) -> a -> (b, a)

What is the type of \t -> (fst . fst $ t, (snd . fst $ t, snd t))?

  1. (a, (b, c)) -> (a, (b, c))
  2. (a, (b, c)) -> ((a, b), c)
  3. ((a, b), c) -> (a, (b, c))

What does the function foldr (\x xs -> xs ++ [x]) [] do?

  1. It doesn’t change its input list at all
  2. It changes the associativity of a list from left to right
  3. It reverses its input list

What does the function foldr (\(x, y) zs -> x : y : zs) [] do?

  1. It turns a list of pairs into a pair of lists
  2. It turns a pair of lists into a list of pairs
  3. It turns a list of pairs into a list of elements

What is the type of foldr (\n b -> n == 3 && b)?

  1. (Foldable t, Eq a, Num a) => Bool -> t a -> Bool
  2. (Foldable t, Eq a, Num a, Bool b) => b -> t a -> b
  3. (Foldable t, Eq a, Num a) => Bool -> [ a ] -> Bool

What is the type of \x -> case x of (True, "Foo") -> show True ++ "Foo"?

  1. Either Bool String -> String
  2. (Bool, String) -> String
  3. Show a => (Bool, String) -> a

Cheatsheet

Thanks again to our TA Daan Wichmann for the following cheatsheet. Note that while this cheatsheet can help you to prepare for the exam, it is non-exhaustive, and your own responsibility that you go through all material.

-- Algebraic datatypes (Enums)

data Weekday = Monday | Tuesday | Wednesday | Thursday | Friday  -- Monday, Tuesday, etc. are constructors
    deriving Show  -- Deriving from another data class

dayIndex :: Weekday -> Int
dayIndex Monday = 0
dayIndex Tuesday = 1
dayIndex Wednesday = 2
dayIndex Thursday = 3
dayIndex Friday = 4

-- Algebraic datatypes with fields

-- Constructor with fields
data Birthdate = Bd Int Int Int  -- Day Month Year

bd = Bd 3 6 2025 :: Birthdate

birthday :: Birthdate -> Int
birthday (Bd day _ _) = day

-- Parameterized types

data UselessWrapper a = Wrapped a

getValue :: UselessWrapper a -> a
getValue (Wrapped x) = x

-- Recursive types

data BinaryTree a = LeafNode a | Node a (BinaryTree a) (BinaryTree a)

getTreeValue :: BinaryTree a -> a
getTreeValue (LeafNode x) = x
getTreeValue (Node x _ _) = x

getLeft :: BinaryTree a -> Maybe a
getLeft (LeafNode _) = Nothing
getLeft (Node _ x _) = Just (getTreeValue x)

getRight :: BinaryTree a -> Maybe a
getRight (LeafNode _) = Nothing
getRight (Node _ _ x) = Just (getTreeValue x)

maxDepth :: BinaryTree a -> Int
maxDepth (LeafNode _) = 0
maxDepth (Node _ x y) = 1 + max (maxDepth x) (maxDepth y)

-- Record syntax

data RecBirthdate = Rbd {day :: Int, month :: Int, year :: Int}
    deriving Show

rbd = Rbd {day = 3, year = 2025, month = 6}

-- Accessor functions
rbday = day rbd  -- Value: 3


-- Keyword: newtype

-- Works the same as data, however it only lets you define one constructor (used for performance reasons)

-- Type alias

type String = [Char]  -- Defines an alias for a type
You have reached the end of this section!

You can check your current points from the blue blob in the bottom-right corner of the page.