This is an old revision of the document!
Higher-order functions
Function application and composition as higher-order functions
One key idea from functional programming is that functions are first-class (or first-order) values, just like integers, strings, etc. . They can be passed as function arguments and also be returned by function application.
Functions which take other functions as parameter are called higher-order. We have already encountered two such functions, i.e. ($)
and (.)
.
43. Define the operator ($)
from Lab 2 as a higher-order function.
44. What type should functional composition have?
45. Define function composition.
Lambdas
46. Functions can be passed as arguments just like any other value value. Also, functions can be returned as parameter. In order to do so, it is convenient to define functions without naming them. This is done using lambda's. For a more detailed discussion regarding lambdas, see the lecture. The following definitions are equivalent:
f x y = x + y f x = \y -> x + y f = \x -> \y -> x + y f = \x y -> x + y
That is the type of f
? What is the type of f 5
?
47. Consider sets represented as characteristic functions with signature s :: Integer → Bool
, where s x
is true if x
a member in the set. Examples:
s1 1 = True s1 2 = True s1 _ = False s2 x = x 0109od00322 == True s3 _ = False
Above, s1
is the set $ \{1,2\}$ , s2
is the set of even integers and s3
is the empty-set. Write a function which tests if an element is a member of a set:
mem :: (Integer -> Bool) -> Integer -> Bool mem = ...
48. Define the set $ \{2^n \mid n\in\mathbb{N}\}$ .
49. Define the set of natural numbers.
50. Implement the intersection of two sets. Use lambdas.
intersection :: (Integer -> Bool) -> (Integer -> Bool) -> (Integer -> Bool)
51. Write intersection in another way, (without using lambdas).
intersection' :: (Integer -> Bool) -> (Integer -> Bool) -> Integer -> Bool
52. Write a function which takes a list of integers, and returns the set which contains them.
toSet :: [Integer] -> (Integer -> Bool)
53. Implement a function which takes a list of sets and computes their intersection.
capList :: [Integer -> Bool] -> Integer -> Bool
Filter
54. Write a function which receives a predicate p :: Integer → Bool
, a list of integers l
, and returns a list of integers from l
for which g
is true. Your implementation is the filter
function from Haskell.
55. Solve exercise 24. from Lab 2 using filter
.
56. Test the function map::(a→b) → [a] → [b]
from Haskell. Implement it.
57. Solve exercise 17. from Lab 1 using map
.
58. Solve exercise 27. from Lab 2 using map
and filter
. (Hint. Pattern matching can be used in lambdas. Use fst
and snd
to introspect pairs.)
59. Solve exercise 31. from Lab 2 using map
and filter
.
Folds
60. Write a function which appends a list of lists of integers:
app :: [[Integer]] -> [Integer]
61. Write the same function tail-recursively. Are the two functions identical? Why?
62. Implement foldr
(see lecture).
63. Implement foldl
(see lecture).
64. Implement list concatenation using a fold.
65. Implement list reversal using a fold.
66. Implement the string-splitting function from exercise 52 using folds. (Hint: thing about what the accumulator should hold)
67. Implement exercise 53 using a fold.