15 Higher Order Functions

Recall that functions are values.

This is called higher order functions

Now we have two functions, but what if we wanted to quadruple a function?

Sure that works, but there are other ways of writing this function!

Now we're doubling x twice. We can do the same thing with the pipeline operator.

Just to clarify, these are all the same!

What if we wanted to raise a number to the fourth? Once again, we can do this a few ways:

Again, these all do the same thing

Notice that in fourth and quad, we're applying a function twice. Let's generalize this in a function twice. This just applies a function twice!

Note the type of twice is ('a -> 'a) -> 'a -> 'a. The ('a -> 'a) is a function that takes in an 'a and returns an 'a. Let's rewrite our quad and fourth functions to use this new twice function.

And once again, to show that these work:

We can actually make this more concise! We can drop the parameter:

These functions do a partial application, then return the function as a result.