What are the types of the following values?
['a','b','c'] :: ?
[Char]
('a','b','c') :: ?
(Char, Char, Char)
[(False,'O'),(True,'1')] :: ?
[(Bool, Char)]
([False,True],['0','1']) :: ?
([Bool], [Char])
Write down definitions that have the following types; it does not matter what the definitions do as long as they are type correct.
bools :: [Bool]
bools = [False, True]
nums :: [[Int]]
nums = [[0,1],[2,3]]
add :: Int -> Int -> Int -> Int
add a b c d = a + b + c + d
copy :: a -> (a,a)
copy x = (x,x)
apply :: (a -> b) -> a -> b
apply :: (a -> b) -> (a -> b)
, so we can use apply f x = f x
What are the types of the following functions?
second xs = head (tail xs)
second :: [a] -> a
swap (x,y) = (y,x)?
swap :: (a,b) -> (b,a)
pair x y = (x,y)?
pair :: a -> b -> (a,b)
double x = x * 2?
double :: Num a => a -> a
palindrome xs = reverse xs == xs?
palindrome :: [a] -> Bool
twice f x = f (f x)?
twice :: (a -> a) -> (a -> a)
Check your answers to the proceeding questions using GHCi.
Omitted.
Why is it not feasible in general for function types to be instances of the
Eq
class? When is it feasible? Hint: two functions of the same type are equal if they always return equal results for equal arguments.
Being an instance of the Eq
class means that the type is comparable. With integers, comparability is no problem β we are taught the strict total ordering of < on the set of the integers with the number line from an early age. However, to discern whether two functions are equal, one must check whether they define the same mappings. This requires operating on the entirety of the domain of both functions, which is computationally expensive.
One can raise additional questions: