Quiz, Cheatsheet and more exercises
Quiz
What’s the type of this function? both p q x = p x && q x
-
a -> Bool -> a -> Bool -> a -> Bool
-
(a -> Bool) -> (a -> Bool) -> a -> Bool
-
(a -> Bool) -> (b -> Bool) -> c -> Bool
What’s the (most general) type of this function? applyInOut f g x = f (g (f x))
-
(a -> b) -> (b -> a) -> a -> b
-
(a -> b) -> (b -> c) -> a -> c
-
(a -> a) -> (a -> a) -> a -> a
Which one of the following functions adds its first argument to the second?
-
f x x = x + x
-
f x = \y -> x + y
-
f = \x y -> x + x
Which one of the following functions does not satisfy f 1 ==> 1
?
-
f x = (\y -> y) x
-
f x = \y -> y
-
f x = (\y -> x) x
Which one of the following functions is correctly typed?
-
f x y = not x; f :: (Bool -> Bool) -> Bool
-
f x = x ++ "a"; f :: Char -> String
-
f x = 'a' : x; f :: String -> String
How many arguments does drop 2
take?
- Zero
- One
- Two
What does this function do? f (_: x:_) = x
- Returns the first element of a list
- Returns an arbitrary element of a list
- Returns all except the first and last elements of a list
- Returns the second element of a list
What is the result of reverse $ take 5 . tail $ "This is a test"
?
-
"i sih"
-
"set a"
- A type error
If f :: a -> b
, then what is the type of map (.f)
?
-
[b -> c] -> [a -> c]
-
[c -> a] -> [c -> b]
-
(b -> c) -> [a -> c]
-
[a] -> [b]
What is the type of the leftmost id
in id id
?
- unspecified
-
a
-
a -> a
-
(a -> a) -> (a -> a)
What is the type of const const
?
- unspecified
-
(c -> a -> b) -> a
-
c -> (a -> b -> a)
-
a -> b -> c -> a
Cheatsheet
Thanks again to our TA Daan Wichmann for the following cheatsheet
-- Higher order functions (functions in functions :o)
higherOrder :: (Int -> Int) -> Int
higherOrder f = f 1
doTwice :: (a -> a) -> a -> a
doTwice f x = f (f x)
-- Higer order functions on lists & Lambdas
addOne :: Int -> Int
addOne x = x + 1
mapped = map addOne [1, 2, 3] -- [2, 3, 4]
mappedLambda = map (\x -> x + 1) [1, 2, 3] -- (\x -> x + 1) is a lambda function
isEven :: Int -> Bool
isEven x = mod x 2 == 0
filtered = filter isEven [1, 2, 3] -- [2]
filteredLambda = filter (\x -> mod x 2 == 0) [1, 2, 3] -- (\x -> mod x 2 == 0) is a lambda function
-- Partial application
add :: Int -> Int -> Int
add x y = x + y
curried = add 3 -- Int -> Int
six = curried 2 -- 5
-- Infix to prefix
code = (+) 120 7 -- 127
zipped = zipWith (+) [0,2,5] [1,3,3] -- [1,5,8]
-- Prefix to infix
even = 2 `mod` 2 == 0 -- True
-- $ and . operator
reversed = head (reverse "abcd")
reversedWithOperator = head $ reverse "abcd" -- Same but using $ operator, if we are lazy / it looks better!
composition f g x = (f . g) x -- This is the same as f (g x)
-- List comprehensions
double = [2*i | i<-[1,2,3]]
-- Output: [2,4,6]
-- Python equivalent: [i for i in range(1,8) if i % 2 == 0]
evens = [i | i <- [1..7], even i]
-- Output: [2,4,6]
-- Python equivalent: [i for i in range(1,8) if i % 2 == 0]
-- takeWhile & dropWhile
firstEvens = takeWhile even [2,4,1,2,3] -- Output: [2, 4]
lastOdds = dropWhile even [1,2,3] -- Output: [1, 2, 3]
containsTwo = elem 2 [1, 2, 7] -- Output: True
-- You can iterate over multiple lists
multipleLists = [ first ++ " " ++ last | first <- ["Bryan", "Emma"], last <- ["(Course Coordinator)","(Lecturer)"] ]
-- Output: ["Byan (Course Coordinator)","Bryan (Lecturer)","Emma (Course Coordinator)","Emma (Lecturer)"]
-- You can add local definitions
localDefs = [ reversed | word <- ["ammE","nayrB","iamaD","naaD", "allE", "aluaP", "zsuetaM"], let reversed = reverse word ]
-- Output: ["Emma", "Bryan", "Damai", "Daan", "Ella", "Paula", "Mateusz"]
-- You can also pattern match in list comprehensions (wowwwww)
patternMatch = [ char | (char:_) <- words "Concepts Of Programming Languages" ] -- Output: "COPL"
-- Custom operators :o
(<+>) :: [Int] -> [Int] -> [Int]
xs <+> ys = zipWith (+) xs ys
(+++) :: String -> String -> String
a +++ b = a ++ " " ++ b
-- Remember typed holes when you get stuck with type errors when working on the exercises!
-- Try replacing a function or variable with a typed hole (_ after the =).
-- It might help you figure out what you need.
Exercises
All exercises can be found in Set3a and Set3b. Please pay attention in the title of the exercise in which file the exercises of this section can be found.
Exercises from 3b:
You can check your current points from the blue blob in the bottom-right corner of the page.