Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 9: More About Higher-Order Functions

Chapter 9: More About Higher-Order Functions

pp. 105-113

Authors

, Yale University, Connecticut
  • Add bookmark
  • Cite
  • Share

Summary

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)

About the book

Access options

Review the options below to login to check your access.

Purchase options

eTextbook
US$79.00
Hardback
US$182.00
Paperback
US$79.00

Have an access code?

To redeem an access code, please log in with your personal login.

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.

Also available to purchase from these educational ebook suppliers