What is "Currying"? Please define currying, provide an example in a language like JavaScript or Haskell. Show a function that takes two arguments, and its curried version that takes one argument and returns a function, and what is "partial application"? How does it relate to currying.
Assistant
We are going to define currying, show an example in JavaScript and Haskell, then explain partial application and its relation to currying.
Definition:
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions, each taking a single argument. In other words, a function that takes two arguments can be transformed into a function that takes one argument and returns another function that takes the second argument.
Example in JavaScript:
Uncurried function:
function add(x, y) {
return x + y;
}
Curried version:
function addCurried(x) {
return function(y) {
return x + y;
};
}
We can also write the curried version using arrow functions for brevity:
const addCurried = x => y => x + y;
Now, we can call the curried function in two steps:
const add5 = addCurried(5);
console.log(add5(3)); // 8
Or directly:
console.log(addCurried(5)(3)); // 8
Example in Haskell:
In Haskell, all functions are curried by default. So a function that takes two arguments is actually a function that takes one argument and returns a function that takes the next argument.
Uncurried (but in Haskell, it's curried by default) function:
add :: Int -> Int -> Int
add x y = x + y
But note: the type Int -> Int -> Int is the same as Int -> (Int -> Int). So add is already curried.
We can use it as:
add 5 3 -- returns 8
Or partially:
add5 = add 5 -- add5 is a function of type Int -> Int
add5 3 -- returns 8
Now, partial application:
Partial application is the process of fixing a number of arguments to a function, producing another function of smaller arity.
In the curried version, we can easily do partial application because the function returns a function when given one argument. However, note that in a language that doesn't curry by default (like JavaScript), we can still achieve partial application by using the curried version.
Relation:
Currying enables partial application. In a curried function, fixing the first argument (by applying the function to one argument) returns a new function that takes the remaining arguments. This is partial application.
But note: partial application can also be done without currying if the language supports it (like with bind in JavaScript for example) but the concept of currying makes partial application straightforward and is the reason why in functional languages partial application is so common.
In summary:
Currying: transforming a multi-argument function into a chain of single-argument functions.
Partial application: applying a function to some, but not all, of its arguments, resulting in a function that takes the remaining arguments.
Example of partial application without currying in JavaScript:
We can use Function.prototype.bind to achieve partial application for any function, curried or not:
But note: bind also sets the this context, which is why we pass null (or undefined in non-strict mode) for the first argument. However, in the case of add, we don't use this.
But the point is: currying is a specific way of structuring functions to allow partial application naturally, but partial application can be achieved by other means as well.
Let's structure the answer:
Definition of currying.
Example in JavaScript (uncurried and curried) and in Haskell.
Definition of partial application.
How currying relates to partial application.
We'll write it in a clear, explanatory manner.
Currying
Currying is the process of transforming a function that takes multiple arguments into a sequence of nested functions, each accepting a single argument. The curried version of a function with n arguments returns a function that takes the first argument and returns another function that takes the second argument, and so on, until all arguments are supplied.
Mathematically, for a function ( f: (A \times B) \to C ), currying produces ( \text{curry}(f): A \to (B \to C) ).
Example in JavaScript
Uncurried Function (takes two arguments)
javascript
function add(x, y) {
return x + y;
}
console.log(add(2, 3)); // 5
Curried Version
javascript
// Curried: takes one argument, returns a function that takes the next
function addCurried(x) {
return function(y) {
return x + y;
};
}
// Or with arrow functions:
const addCurried = x => y => x + y;
// Usage:
const addTwo = addCurried(2); // Partially applied: returns a function expecting `y`
console.log(addTwo(3)); // 5
console.log(addCurried(2)(3)); // 5 (direct chaining)
Example in Haskell
In Haskell, all functions are curried by default. A function declared with multiple parameters is implicitly curried.
haskell
-- Type: Int -> Int -> Int (equivalent to Int -> (Int -> Int))
add :: Int -> Int -> Int
add x y = x + y
-- Usage:
add 2 3 -- 5
addTwo = add 2 -- Partial application: addTwo :: Int -> Int
addTwo 3 -- 5
Partial Application
Partial application is the act of supplying some (but not all) arguments to a function, producing a new function that accepts the remaining arguments.
It is possible with curried functions because each argument application returns a new function waiting for the next argument.
It can also be achieved with uncurried functions via utilities like Function.bind in JavaScript, but currying makes it syntactically direct.
Relation Between Currying and Partial Application
Currying enables partial application:
A curried function’s structure (returning a new function after each argument) naturally supports partial application. Supplying fewer than n arguments automatically yields a partially applied function.
Partial application does not require currying:
You can partially apply an uncurried function using language features (e.g., bind), but this often involves fixing arguments in a specific position and may require handling this or placeholder values.
Currying is a transformation; partial application is an action:
Currying: converts a multi-arg function into nested single-arg functions.
Partial application: uses a function (curried or not) by pre-filling some arguments.
Key Distinction in Code
JavaScript: Partial application without currying (using bind)
javascript
function multiply(x, y) {
return x * y;
}
const double = multiply.bind(null, 2); // Fix first argument as 2
console.log(double(5)); // 10
Here, bind creates a partially applied function but doesn’t curry multiply (it remains a 2-arg function under the hood).
Contrast with Currying
javascript
const multiplyCurried = x => y => x * y;
const doubleCurried = multiplyCurried(2); // Naturally partial: returns `y => 2*y`
Summary
Concept
What It Is
Example
Currying
Transform f(a,b,c) → f(a)(b)(c)
addCurried = x => y => x + y
Partial App.
Fix some arguments early → get a function of the remaining arguments
const add5 = addCurried(5)
Relation
Currying facilitates partial application by design; partial application uses currying (or other tools) to pre-fill arguments.
In functional languages like Haskell, where functions are curried by default, partial application is a natural and common pattern. In non-curried languages (e.g., JavaScript), explicit currying (or libraries like Lodash’s curry) is needed to make partial application as seamless.