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. ===== Higher-order functions ===== In our previous examples, we have seen functions such as ''(+)'' or ''(*)'' which are passed as parameter to other functions, e.g. ''foldr''. //A function which expects another function as parameter is called a **higher-order function**//. One distinctive functional programming trait relies on defining and using higher-order functions. Haskell makes this programming style very natural. Functions can be defined in several ways, e.g. <code> f x = x + 1 </code> as **anonymous functions**: <code> f = \x -> x + 1 </code> (which is read //"lambda x ..."//) or using **pattern matching**: <code> f 0 = 1 f 1 = 2 f 2 = 3 </code> ==== Currying ==== Consider the following function definitions: <code> f1 x y = x + y f2 x = \y -> x + y f3 = \x -> \y -> x + y f4 = \x y -> x + y </code> Read syntactically: * the first function expects two parameters and returns their additions * the second function **returns a function which adds y to x**. For instance ''(f2 4)'' is a function which adds ''4'' to an integer * the third function is similar to the previous one, but defined as an anonymous function * the fourth function is an anonymous function definition with two parameters **There is no conceptual or functional difference between //any// of the above definitions**. From a //Haskell compiler's view// all function definitions look exactly as the third one. For instance, ''(f1 1)'' is a correct function call which will return a function adding ''1'' to its parameter. ''(f1 1)'' is called a **functional closure**, because it creates (closes) a context in which the first parameter is equal to the value ''1''. At the same time, the function call ''((f1 1) 2)'' is correct, returns ''3'', and is equivalent to ''(f1 1 2)''. More generally, functions defined as ''f2'' or ''f3'' are called **curried (or curry) functions** (in honor of Haskell Curry, a mathematician which contributed to functional programming) --- they receive parameters //in turn//. The other definitions are called **uncurried**. We again stress that, in Haskell, there is no difference between curried and uncurried functions. This is **not the case** in other functional languages, e.g. Lisp, Scheme. ==== Other higher-order functions ==== Imagine we want to do a certain computation on each member of a list, individually. Let's say, double each element. If we think functionally, we want to take a list ''[a,b,c, ...]'', a function ''f'', and build the list: <code> [f a, f b, f c, ...] </code> It turns out we can write such a function relying on fold: <code haskell> pp_map f = foldr (\x y->(f x):y) [] </code> There is also a shorter way to write ''pp_map'': <code> pp_map f = foldr ((:).f) [] </code> In the expression ''(:).f'', ''.'' (the dot) stands for functional composition. Consider the anonymous function: <code> \x->x+1 </code> which takes a parameter ''x'' and returns its natural successor. We can compose it as follows: <code> (\x -> 2*x).(\x->x+1) </code> to obtain the function ''\x -> 2*(x+1)''. In general, the function call ''(f.g) x'' is equivalent to ''f (g x)''. To understand ''(:).f'' let us write the cons ''(:)'' as an anonymous function: <code> \h t -> h:t </code> Hence, the following expressions are equivalent: <code> ((:).f) ((\h t -> h:t).f) ((\h->\t->h:t).f) (\t->(f h):t) </code> A natural mistake is to consider ''((:).f)'' as invalid, since it receives two parameters, and we cannot compose such a function with one receiving only one parameter. However, every function in Haskell receives one parameter and returns **a value** (which can be a function or a primitive value). There are other examples of useful higher-order functions. We illustrate them using examples: <code> filter (>3) [1,2,3,4,5,2] zipWith (+) [1,2,3] [3,2,1] </code> Guess what each function does, by testing it on several lists. ==== Haskell implementation of foldl ==== In Haskell, ''foldl'' is implemented as follows: <code> foldl op acc [] = acc foldl op acc (h:t) = foldl op (op acc h) t </code> Note the call of the ''op'' function. ==== An exercise in modular programming with higher-order functions ==== Let us consider the task of matrix multiplication in Haskell. Example: <code> 1 2 3 1 0 0 1+3 2 2+3 4 2 5 4 5 6 x 0 1 1 = 4+6 5 5+6 = 10 5 11 7 8 9 1 0 1 7+9 8 8+9 16 8 17 </code> === Matrix representation in Haskell === The basic representation building-block in Haskell is the list. We can represent matrices in Haskell as a list where each element is a row (hence a list of elements). Example: <code haskell> m = [[1,2,3],[4,5,6],[7,8,9]] </code> A good warmup exercise is to write a nice display function for matrices. We transform each element of a line into a string: <code haskell> displayline l = map (\e->(show e)++" ") l </code> The code can be improved for legibility: <code haskell> displayline = map ((++" ").show) </code> Next, we fold the list into a string with a newline character: <code haskell> displayline l = foldr (++) "\n" (map ((++" ").show) l) </code> and simplify again, by expressing the pipeline function calls as functional composition: <code haskell> displayline :: Show a => [a] -> [Char] displayline = (foldr (++) "\n") . (map ( (++ " ") . show ) ) </code> Note that we need to explicitly state the type of display. Sometimes, in Haskell, an explicit type declaration is required. For now, we omit details. Next, we apply the above process on all matrix lines: <code haskell> display :: Show a => [[a]] -> [[Char]] display = let bind = foldr (++) "\n" in bind.(map (bind . (map ((++" ") . show ) ) ) ) </code> Notice that we have separated the binding process, because we reuse it. Finally, we make all matrices displayable: <code haskell> instance (Show a) => Show [[a]] where show = let bind = foldr (++) "\n" in bind.(map (bind.(map ((++" ").show ) ) ) ) </code> More details about this implementation (e.g. instances, classes) will be given in future lectures. === Matrix multiplication === == Step 1: Transposition == Matrix multiplication operates on the **lines** of the first matrix and **columns** of the second. We transpose the second matrix, so that we now operate on lines on both matrices. The following code extracts **the first line** from a matrix ''m'': <code haskell> map head m </code> A matrix ''m'' **without** its first column is: <code haskell> map tail m </code> Finally transposition is given by: <code haskell> transpose ([]:_) = [] transpose m = (map head m) : transpose (map tail m) </code> Notice that the //basis case// corresponds to a **list containing empty lists**. == Step 2: Computing multiplication == To compute the ''i,j''th **element** of the multiplication matrix, we need to multiply per element the ''i''th line by the ''j''th column: <code haskell> zipWith (*) li cj </code> and then add-up the values: <code haskell> foldr (+) 0 (zipWith (*) li cj) </code> To obtain the ''i''th **line** of the multiplication matrix, we need to repeat the above process **for each column of the second matrix**, in other words, for each line of its transposition: <code haskell> map (\col -> foldr (+) 0 (zipWith (*) li col) ) (transpose m2) </code> Finally, to obtain the multiplication matrix, we need to compute all its lines, hence: <code haskell> mult m1 m2 = map (\line -> map (\col -> foldr (+) 0 (zipWith (*) line col) ) (transpose m2) ) m1 </code> === Matrices as images === A matrix can be used to represent a **rasterized image** (a collection of pixels). In this example, we consider that pixels can have values: ' ' (white), '.' (grey) and '*' (black). Higher-order functions can be naturally used to represent image-transformations, for instance, flipping: <code haskell> flipH = map reverse flipV = reverse </code> Rotations: <code haskell> rotate90left = flipV.transpose rotate90right = flipH.transpose </code> The //negative// of an image: <code haskell> invert = map (map (\x->if x=='*' then ' ' else '*') ) </code> Scaling an image horizontally: <code haskell> scalex = foldr (\h t->h:h:t) [] </code> Scaling vertically: <code haskell> scaley = map scalex </code> Balanced scale: <code haskell> scale = scalex . scaley </code> Or just a random sequence of operations: <code haskell> rand = foldr (.) id [rotate90left, invert, scale] </code>