Safe Haskell | Safe |
---|
Solutions to Exercises for November 9, 2018
Exercise 3
thrice :: (a -> a) -> a -> a Source #
thrice f x = f (f (f x))
Since f
is applied to the result of f x
, the input as well as the
output of f
need to be of the same type as x
. There are no further
restrictions. Hence the type of thrice
is (a -> a) -> a -> a
.
mapPair :: (a -> b) -> (a, a) -> (b, b) Source #
mapPair f (x, y) = (f x, f y)
The function f
is applied to both components x
and y
of the input pair.
Thus, both have to be of the same type. Moreover, the output pair consists of
two components that result from applying f
. Again, this yields that both have
to be of the same type. There are no further restrictions. In particular, there
are not restrictions on f
, except for it being a function. Hence, the type of
f
is a -> b
. Taken together, this gives the type (a -> b) -> (a, a) -> (b,
b)
for mapPair
.
Exercise 4
Using the definition
filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs
The expression filter (const False) ["a","b","c"]
can be evaluated as follows (where the
justification for applying conditional equations resulting from guarded
patterns are given on the right, separated by <==
):
filter (const False) ["a","b","c"] = filter (const False) ["b","c"] <== const False "a" /= True = filter (const False) ["c"] <== const False "b" /= True = filter (const False) [] <== const False "c" /= True = []
Exercise 5
concat :: [[a]] -> [a] Source #
One possible definition is
concat [] = [] concat (x:xs) = x ++ concat xs
Again, we could use foldr
to obtain the shorter definition
concat = foldr (++) []
Exercise 6
intercalate :: [a] -> [[a]] -> [a] Source #
Note the special treatment of singleton lists (that is, lists having exactly one element), which avoids the "separator" to be appended after the last list.