Higher order Functions
1 Parentheses!? atHome
Give examples for functions with the following types:
Float −> Float) −> Float
(Float −> (Float −> Float)
Float −> Float) −> (Float −> Float) (
Give an example program p
with the following type and explain how the function arrow ->
associates:
p :: Int -> Bool -> Int
Give examples of programs foo
, bar
and baz
, such that foo bar baz
type checks and please explain how function application associates.
Can you give examples of programs foo
, bar
and baz
, such that one might expect foo bar baz
to be well-typed if function application were to associate in the opposite direction, but such
that foo bar baz
is in reality ill-typed?
2 Currying inClass
Please implement two functions
curry :: ((a, b) -> c) -> (a -> b -> c)
uncurry :: (a -> b -> c) -> ((a, b) -> c)
Can you convince yourself that curry
and uncurry
are mutually inverse? Try it out on some example functions!
Please simplify the types of curry
and uncurry
by removing unnecessary parentheses
3 Filter atHome
The function filter
can be defined in terms of concat
and map
:
filter p = concat . map box
where box x = _
Complete the definition of box
.
4 Function Composition atHome
Function composition first applies the latter of the supplied
functions to the argument, the former thereafter. Write a function
before
that can be used to rewrite f . g . h
to h `before` g
`before` f
. What can you say about associativity of (.)
and
before
?
5 Higher order data types atHome challenging
We have seen that [...]
is a type function that maps types to
types. Similarly because −>
is a type constructor mapping two types
to a type, for some c
also c −>
is a type function mapping a type
a
to c −> a
. Rewrite the type of map
by substituting the type
function [...]
by c −>
. Can you derive an implementation from the
resulting type?