Quiz and cheatsheet
What is the type of swap . swap
?
-
(a, b) -> (a, b)
-
(a, b) -> (b, a)
-
a -> a
What is the type of \f g x -> (f x, g x)
?
-
(a -> b) -> (c -> d) -> (a,c) -> (b, d)
-
(a -> b) -> (a -> c) -> a -> (b, c)
-
(a -> b) -> (b -> a) -> a -> (b, a)
What is the type of \t -> (fst . fst $ t, (snd . fst $ t, snd t))
?
-
(a, (b, c)) -> (a, (b, c))
-
(a, (b, c)) -> ((a, b), c)
-
((a, b), c) -> (a, (b, c))
What does the function foldr (\x xs -> xs ++ [x]) []
do?
- It doesn’t change its input list at all
- It changes the associativity of a list from left to right
- It reverses its input list
What does the function foldr (\(x, y) zs -> x : y : zs) []
do?
- It turns a list of pairs into a pair of lists
- It turns a pair of lists into a list of pairs
- It turns a list of pairs into a list of elements
What is the type of foldr (\n b -> n == 3 && b)
?
-
(Foldable t, Eq a, Num a) => Bool -> t a -> Bool
-
(Foldable t, Eq a, Num a, Bool b) => b -> t a -> b
-
(Foldable t, Eq a, Num a) => Bool -> [ a ] -> Bool
What is the type of \x -> case x of (True, "Foo") -> show True ++ "Foo"
?
-
Either Bool String -> String
-
(Bool, String) -> String
-
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.