£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Partial Application and Currying
7
WIM: Hello, everyone. In this short video, we are going to introduce two new concepts that are slightly more advanced. And they are currying and partial application. So consider the function signature like this, where we have a type X, Y, Z, and return value of type A. So this is a function that takes three arguments of three different types and produces a return value of type A. However, the arrow here in this type signature is meaningful. And also, it has a certain associativity. In fact, it associates to the right.
55.3
And that means that, actually, we can write this expression also like this, which means that we actually can consider f as a function that takes an element of type X and returns a function of Y to function of Z to A. Similar, this return function, Y, can be considered both as a function of Y and Z to A or as a function of Y to Z to A. So this idea that you can always rewrite the function of a single argument returning another function is called currying. To illustrate this on an actual function, it’s easiest to use lambda functions. So suppose we have a lambda function of X, Y, and Z, and function body is completely irrelevant.
109.6
Then to rewrite this, so that this becomes a function of a single argument. We create a new lambda function, which is this one. So the body remains completely untouched. And we can rewrite this inner function once more.
129.2
And in this way, we have created a function of a single argument which returns a function of a single argument which returns a function of a single argument, which eventually returns the value. So this is the typical technique known as currying you use to transform multi-argument functions into single argument functions. So it’s named after the logician Haskell Curry. But actually, he wasn’t the first to invent this. The first one to invent this technique was called Moses Schonfinkel, another logician. But well, his name wasn’t so catchy, so they decided to go with currying. Another concept closely related to currying is that of partial application.
172.1
For example, consider the function sq, which takes X and Y and returns the sum of the squares.
184.6
This function can actually, again, be slightly rewritten as– because the function application, actually, is left associative. That means that what we have here is a function in its own right that operates on Y. So we can do other things like, for instance, say OK. Let’s define sq4 as sq4. And now sq4 is a new function which just takes one argument. If we now call Sq4 on value three, it will return 25. This is an example of partial application. So basically, we’ve applied four to the function argument x, but we haven’t applied anything to y, and the result is a new function. And then we can use this new function for doing our computation.
246.1
So this technique of partial application is really used a lot in functional programming in Haskell, and it’s the reason why you can write things, for instance, like this.
260.1
So here, the times two is partial application of the function multiplication that we use as an operator here, so it takes two arguments and returns an argument. We have applied it partially, using the two. We get a new function, which we can use in the map, because map requires functions with a single argument. And so we have a function that doubles the elements of a list.
285.6
So this was just to give you a quick intuition on these key concepts of currying and partial application.

Partial Application and Currying

Currying

Consider a type signature of a function with three arguments:

 f :: X -> Y -> Z -> A

The arrow “->” is right-associative, so this is the same as:

 f :: X -> (Y -> (Z -> A))

What this means is that we can consider f as a function with a single argument of type X which returns a function of type Y->Z->A.

The technique of rewriting a function of multiple arguments into a sequence of functions with a single argument is called currying. We can illustrate this best using a lambda function:

 x y z -> ... x -> (y z -> ...) x -> (y -> (z -> ... )) 

The name “currying”, is a reference to logician Haskell Curry. The concept was actually proposed originally by another logician, Moses Schönfinkel, but his name was not so catchy.

Partial application

Partial application means that we don’t need to provide all arguments to a function. For example, given

 sq x y = x*x+y*y

We note that function application associates to the left, so the following equivalence holds

 sq x y = (sq x) y

We can therefore create a specialised function by partial application of x:

 sq4 = sq 4 -- = y -> 16+y*y sq4 3 -- = (sq 4) 3 = sq 4 3 = 25

This is why you can write things like:

 doubles = map (*2) [1 .. ]