Part 17

Quiz, Cheatsheet and more exercises

Quiz

What’s the type of this function? both p q x = p x && q x

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

What’s the (most general) type of this function? applyInOut f g x = f (g (f x))

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

Which one of the following functions adds its first argument to the second?

  1. f x x = x + x
  2. f x = \y -> x + y
  3. f = \x y -> x + x

Which one of the following functions does not satisfy f 1 ==> 1?

  1. f x = (\y -> y) x
  2. f x = \y -> y
  3. f x = (\y -> x) x

Which one of the following functions is correctly typed?

  1. f x y = not x; f :: (Bool -> Bool) -> Bool
  2. f x = x ++ "a"; f :: Char -> String
  3. f x = 'a' : x; f :: String -> String

How many arguments does drop 2 take?

  1. Zero
  2. One
  3. Two

What does this function do? f (_: x:_) = x

  1. Returns the first element of a list
  2. Returns an arbitrary element of a list
  3. Returns all except the first and last elements of a list
  4. Returns the second element of a list

What is the result of reverse $ take 5 . tail $ "This is a test"?

  1. "i sih"
  2. "set a"
  3. A type error

If f :: a -> b, then what is the type of map (.f)?

  1. [b -> c] -> [a -> c]
  2. [c -> a] -> [c -> b]
  3. (b -> c) -> [a -> c]
  4. [a] -> [b]

What is the type of the leftmost id in id id?

  1. unspecified
  2. a
  3. a -> a
  4. (a -> a) -> (a -> a)

What is the type of const const?

  1. unspecified
  2. (c -> a -> b) -> a
  3. c -> (a -> b -> a)
  4. 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 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.