Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 3. 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 [[pp:l02|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: <code haskell> f x y = x + y f x = \y -> x + y f = \x -> \y -> x + y f = \x y -> x + y </code> 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: <code haskell> s1 1 = True s1 2 = True s1 _ = False s2 x = x `mod` 2 == True s3 _ = False </code> Above, ''s1'' is the set $math[\{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: <code haskell> mem :: (Integer -> Bool) -> Integer -> Bool mem = ... </code> 48. Define the set $math[\{2^n \mid n\in\mathbb{N}\}]. 49. Define the set of natural numbers. 50. Implement the intersection of two sets. Use lambdas. <code haskell> intersection :: (Integer -> Bool) -> (Integer -> Bool) -> (Integer -> Bool) </code> 51. Write intersection in another way, (without using lambdas). <code haskell> intersection' :: (Integer -> Bool) -> (Integer -> Bool) -> Integer -> Bool </code> 52. Write a function which takes a list of integers, and returns the set which contains them. <code haskell> toSet :: [Integer] -> (Integer -> Bool) </code> 53. Implement a function which takes a list of sets and computes their intersection. <code haskell> capList :: [Integer -> Bool] -> Integer -> Bool </code> = === 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 [[pp:l02|Lab 2]] using ''filter''. 56. Test the function ''map::(a->b) -> [a] -> [b]'' from Haskell. Implement it. 57. Solve exercise 17. from [[pp:l01|Lab 1]] using ''map''. 58. Solve exercise 27. from [[pp:l02|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 [[pp:l02|Lab 2]] using ''map'' and ''filter''. ==== Folds ==== 60. Write a function which appends a list of lists of integers: <code haskell> app :: [[Integer]] -> [Integer] </code> 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 function from exercise 52 using folds. (Hint: thing about what the accumulator should hold) 67. Implement exercise 53 using a fold.