Hello! Welcome back to Haskell Function of the Day! Today we are going to look at a function with a very strange name: . Yes that's just a single dot. It is an infix operator. Let's check it's type > :type (.) (.) :: (b -> c) -> (a -> b) -> a -> c So (.) takes two paramters, the first of which is a function from beta to gamma, the second of which is a function from alpha to beta, and it returns a function from alpha to gamma. It's not necessary to parenthesize the final -> operator because in types -> is right associative. Note that "alpha" is the type 'a', "beta" is the type 'b', and "gamma" is the type 'c'. It is traditional to use greek letters for type variables in the theory of programming languages. What does (.) do? It's function composition. As an example: > let f x = x + 1 > let g x = 2*x The above defines two simple functions. Now consider something like: > g (f 2) 6 This applies g to the result computed by f. Of course we could also do > g (f 10) 22 But what if we wanted to do this kind of "composition" of functions regularly. We could define a new function like this > let h x = g (f x) What the . function does is allow us to define h in a very simple and elegant way. > h = g . f This says h is the composition of functions g and f such that h applied to something is just the result of applying f to that thing followed by applying g to f's result. Notice that the actual paramter (say x) is not mentioned in the definition of h above. This is an example of "point free" programming (despite the use of function .). Since the result of function composition is a function it can be used in places where a function is expected. For example. > map (g . f) [1, 2, 3, 4] [4, 6, 8, 10] The function being mapped across the list is (g . f). If you wanted to do this using a "pointful" style it would be > map (\x -> g (f x)) [1, 2, 3, 4] [4, 6, 8, 10] However, the introduction of the parameter doesn't seem very helpful here. Consider this potentially more useful example > map ((+1) . length) ["Hello", "you"] [6, 4] This returns a list where each element is an integer that is one more than the length of the string in the corresponding element of the input. In this case we are composing the function (+1) and length. Here length has type String->Int and (+1) has type Int->Int. However the composition works because the result of length is the same type as the parameter to (+1). The pointful version of the last example looks like > map (\s -> (length s) + 1) ["Hello", "you"] [6, 4] The main virtue of the composition function isn't in that it saves you a few characters typing. Rather it's value is that it encourages you to think about your problem at the level of functional transformation rather than at the level of specific data values. For example, suppose I want to transform a list of strings by first mapping the strings to their lengths and then checking to see of all lengths are greater than 10. Here is such a function > let checkLengths = all (>10) . map length The function checkLengths, written point-free, first applies 'map length' to the input list and then applies 'all (>10)' to the list that is produced. The result is a Bool that is true only if all the lengths are greater than 10. > checkLengths ["Hello", "super fantastic function madness"] False > checkLengths ["Haskell is cool", "super fantastic function madness"] True As an aside notice that every term in the expression 'all (>10) . map length' is a function! This is a point we'll return to later when we look at the lambda calculus. That's it for today's edition of Haskell Function of the Day. I hope you enjoyed it! Peter