You have now seen several examples where functions are passed as arguments to other functions, such as with fold and map. In this chapter, I will show several examples where functions are also returned as values. This will lead to several techniques for improving definitions that we have already written - techniques that we will use often in the remainder of the text.
Currying
The first improvement relates to the notation we have used to write function applications, such as simple x y z. Although I have noted the similarity of this to the mathematical notation simple(x, y, z), in fact, there is an important difference, namely that simple x y z is actually equivalent to (((simple x) y) z). In other words, function application is left associative, taking one argument at a time.
Let's look at the expression (((simple x) y) z) a bit closer: There is an application of simple to x, the result of which is applied to y; so (simple x) must be a function! The result of this application, ((simple x) y), is then applied to z, so ((simple x) y) must also be a function!
Because each of these intermediate applications yields a function, it seems perfectly reasonable to define a function such as:
multSumByFive = simple 5
What is simple 5? From the above argument, we know that it must be a function. And from the definition of simple in Section 1.1, we might guess that this function takes two arguments, and returns 5 times their sum.
Indeed, we can calculate this result as follows:
multSumByFive a b
⇒ (simple 5) a b
⇒ simple 5 a b
⇒ 5 * (a + b)
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.