Type inference
In these exercises you should assume the following types:
(+) :: Int -> Int -> Int
even :: Int -> Bool
head :: [a] -> a
(++) :: [a] -> [a] -> [a]
foldr :: (a -> b -> b) -> b -> [a] -> b
map :: (a -> b) -> [a] -> [b]
concat :: [[a]] -> [a]
(.) :: (b -> c) -> (a -> b) -> a -> c
1 atHome
What is the type of head ([3, 2] ++ [2])
? Give the type derivation.
2 atHome
What is the type of (+) 3
? Give the type derivation.
3 atHome
What is the type of map even
? Give the type derivation.
4 atHome
What is the type of map concat
? Give the type derivation.
5 inClass
What is the type of map head
? Give the type derivation.
6 atHome
What is the type of reverse . reverse
? Give the type derivation.
7 atHome
What is the type of foldr (+)
? Give the type derivation.
8 inClass
What is the type of foldr map
? Give the type derivation.
[a] -> [a -> a] -> [a]
[a] -> [[a -> a]] -> [a]
[a] -> [[a -> a] -> [a]]
[[a]] -> [a -> a] -> [a]
9 atHome
What is the type of map . foldr
? Give the type derivation.
(a -> a -> a) -> [a] -> [[a] -> a]
(a -> a -> a) -> [b] -> [b -> a]
(b -> a -> a) -> [a] -> [[b] -> a]
(b -> a -> a) -> [b] -> [[a] -> a]
10 atHome
Which of the following is the type of concat . concat
? Give the type derivation.
[[a]] -> [[a]] -> [[a]]
[[a]] -> [[a]] -> [a]
[[[a]]] -> [a]
[a] -> [[a]] -> [a]
11 atHome
What is the type of map map
? Give the type derivation.
12 inClass
What is the type of map (map map)
? Give the type derivation.
[[a -> b]] -> [[[a] -> [b]]]
[a -> b] -> [[[a] -> [b]]]
[[a -> b]] -> [[[a -> b]]]
[[a -> b] -> [[a] -> [b]]]
Give the type derivation.
13 atHome
Which observation is correct when comparing the types of (map map)
map
and map (map map)?
- The type of the first is less polymorphic than the type of the second.
- The type of the first is more polymorphic than the type of the second.
- The types are the same, since function composition is associative.
- One of the expressions does not have any type at all.
14 atHome
The function maximum
has type Ord a => [a] -> a
. What is the type
of map maximum
? Can you give a derivation?