By Ben

2008-08-30 20:12:55 8 Comments

I've seen references to curried functions in several articles and blogs but I can't find a good explanation (or at least one that makes sense!)


@pr-pal 2019-10-22 03:59:38

This is equivalent to what partial does in python. When we want to use a generic function for a focussed purpose, we fix a subset of parameters using this mechanism.

@Prashant Andani 2019-10-08 16:20:45

Here is the example of generic and the shortest version for function currying with n no. of params.

const add = a => b => b ? add(a + b) : a; 

const add = a => b => b ? add(a + b) : a; 

@Kyle Cronin 2008-08-30 20:19:51

Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument. Here's an example in JavaScript:

function add (a, b) {
  return a + b;

add(3, 4); // returns 7

This is a function that takes two arguments, a and b, and returns their sum. We will now curry this function:

function add (a) {
  return function (b) {
    return a + b;

This is a function that takes one argument, a, and returns a function that takes another argument, b, and that function returns their sum.


var add3 = add(3);


The first statement returns 7, like the add(3, 4) statement. The second statement defines a new function called add3 that will add 3 to its argument. This is what some people may call a closure. The third statement uses the add3 operation to add 3 to 4, again producing 7 as a result.

@Strawberry 2013-08-08 18:00:42

In a practical sense, how can I make use this concept?

@nyson 2013-10-26 16:52:04

@Strawberry, say for instance that you have a list of numbers in a [1, 2, 3, 4, 5] that you wish to multiply by an arbitrary number. In Haskell, I can write map (* 5) [1, 2, 3, 4, 5] to multiply the whole list by 5, and thus generating the list [5, 10, 15, 20, 25].

@Strawberry 2013-10-26 23:11:24

I understand what the map function does, but I'm not sure if I understand the point you're trying to illustrate for me. Are you saying the map function represents the concept of currying?

@Doval 2014-01-17 15:22:10

@Strawberry The first argument to map must be a function that takes only 1 argument - an element from the list. Multiplication - as a mathematical concept - is a binary operation; it takes 2 arguments. However, in Haskell * is a curried function, similar to the second version of add in this answer. The result of (* 5) is a function that takes a single argument and multiplies it by 5, and that allows us to use it with map.

@Doval 2014-01-17 15:25:54

@Strawberry The nice thing about functional languages like Standard ML or Haskell is that you can get currying "for free". You can define a multi-argument function as you would in any other language, and you automatically get a curried version of it, without having to throw in a bunch of lambdas yourself. So you can produce new functions that take less arguments from any existing function without much fuss or bother, and that makes it easy to pass them to other functions.

@Mickey Tin 2015-01-04 00:31:18

@Danny 2015-09-23 15:03:41

I still don't quite understand why you would want to do this.

@lukas_o 2016-01-27 10:02:36

@Strawberry probably for job interviews. ;)

@SSH This 2016-01-29 21:57:06

Well thank you for the explanation, unfortunately the Scheme examples flew right over my head.

@Kyle Cronin 2016-01-30 02:02:15

@SSHThis Do you know JavaScript? I edited this answer to use JS instead of Scheme.

@Zeek Aran 2016-02-05 20:00:38

I'm a little late, but your code says 3+7=7. Thank you for the explanation though!

@semicolon 2016-03-31 02:12:34

@Danny well what is more readable and concise? (lambda x: x + 5), (\x -> x + 5) etc. or (+ 5)? Because without currying you have to use the former for things like map (+ 5) [1, 2, 3, 4, 5].

@Kyle Cronin 2016-10-02 16:50:03

@OscarRyz That doesn't look like currying to me. If you want to go with filtering, something like var greaterThan = x => y => y > x; would let you curry greaterThan so that you can use it like [1,2,3,4,5].filter(greaterThan(3)).

@aeid 2017-03-22 11:25:13

basically its a use case of closures .. close over one or more argument to make a more specialized function .

@aw04 2017-04-27 20:36:15

it's useful for caching arguments, maybe you need to call a function many times but the first argument will be the same or maybe that first argument maintains some sort of internal state in a closure

@camjocotem 2019-01-28 14:08:59

Currying is useful if you find you have a function where you are pass in a parameter which never changes. (Possibly a class with lots of reusable methods?) Instead of always passing in that same parameter, you curry the function to only pass in the parameters that do change

@Adzz 2016-01-14 17:05:54

It can be a way to use functions to make other functions.

In javascript:

let add = function(x){
  return function(y){ 
   return x + y

Would allow us to call it like so:

let addTen = add(10);

When this runs the 10 is passed in as x;

let add = function(10){
  return function(y){
    return 10 + y 

which means we are returned this function:

function(y) { return 10 + y };

So when you call


you are really calling:

 function(y) { return 10 + y };

So if you do this:


it's the same as:

function(4) { return 10 + 4} // 14

So our addTen() always adds ten to whatever we pass in. We can make similar functions in the same way:

let addTwo = add(2)       // addTwo(); will add two to whatever you pass in
let addSeventy = add(70)  // ... and so on...

@user7125259 2018-03-16 07:02:13

The best step by step explanation of an inherently sequential process I've seen here, and perhaps the best, most explanatory answer of the lot.

@whitneyland 2018-04-18 23:46:21

@jonsilver I’d say the opposite, not a good explanation. I agree it’s good at explaining the example posed, but people tend to default to thinking, “yeah perfectly clear but I could have done the same thing another way so what good is currying?” In other words, I wish it had just enough context or explanation to illuminate not just how currying works, but also why it’s not a useless and trivial observation compared to other ways of adding ten.

@MidhunKrishna 2019-03-26 12:49:15

Currying is translating a function from callable as f(a, b, c) into callable as f(a)(b)(c).

Otherwise currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments.

Literally, currying is a transformation of functions: from one way of calling into another. In JavaScript, we usually make a wrapper to keep the original function.

Currying doesn’t call a function. It just transforms it.

Let’s make curry function that performs currying for two-argument functions. In other words, curry(f) for two-argument f(a, b) translates it into f(a)(b)

function curry(f) { // curry(f) does the currying transform
  return function(a) {
    return function(b) {
      return f(a, b);

// usage
function sum(a, b) {
  return a + b;

let carriedSum = curry(sum);

alert( carriedSum(1)(2) ); // 3

As you can see, the implementation is a series of wrappers.

  • The result of curry(func) is a wrapper function(a).
  • When it is called like sum(1), the argument is saved in the Lexical Environment, and a new wrapper is returned function(b).
  • Then sum(1)(2) finally calls function(b) providing 2, and it passes the call to the original multi-argument sum.

@Anon 2009-08-30 17:47:50

Here's a toy example in Python:

>>> from functools import partial as curry

>>> # Original function taking three parameters:
>>> def display_quote(who, subject, quote):
        print who, 'said regarding', subject + ':'
        print '"' + quote + '"'

>>> display_quote("hoohoo", "functional languages",
           "I like Erlang, not sure yet about Haskell.")
hoohoo said regarding functional languages:
"I like Erlang, not sure yet about Haskell."

>>> # Let's curry the function to get another that always quotes Alex...
>>> am_quote = curry(display_quote, "Alex Martelli")

>>> am_quote("currying", "As usual, wikipedia has a nice summary...")
Alex Martelli said regarding currying:
"As usual, wikipedia has a nice summary..."

(Just using concatenation via + to avoid distraction for non-Python programmers.)

Editing to add:

See, which also shows the partial object vs. function distinction in the way Python implements this.

@Eric M 2009-08-30 21:46:27

I do not get this - you do this: >>> am_quote = curry(display_quote, "Alex Martelli") but then you do this next: >>> am_quote("currying", "As usual, wikipedia has a nice summary...") So you have a function with two args. It would seem that currying should give you three different funcs that you would compose?

@Anon 2009-08-31 01:43:39

I am using partial to curry only one parameter, producing a function with two args. If you wanted, you could further curry am_quote to create one that only quoted Alex on a particular subject. The math backgound may be focused on ending up with functions with only one parameter - but I believe fixing any number of parameters like this is commonly (if imprecisely from a math standpoint) called currying.

@Anon 2009-08-31 02:20:16

(btw - the '>>>' is the prompt in the Python interactive interpreter, not part of the code.)

@Eric M 2009-08-31 03:49:04

OK thanks for the clarification about args. I know about the Python interpreter prompt, I was trying to quote the lines but it diidn't work ;-)

@Anon 2009-08-31 04:47:45

After your comment, I searched and found other references, including here on SO, to the difference between "currying" and. "partial application" in response to lots of instances of the imprecise usage I'm familiar with. See for instance:…

@Eric M 2009-08-31 15:05:57

@Anon: I just re-read your example and saw that I missed the point. The first arg to display_quote() is after currying to produce am_quote() not present in am_quote(), but the value is carried. If I decompose display_quote() by hand I can produce 3 functions that could be composed to get the same result as display_quote() without fixing the values of any args. The call am_quote = curry(display_quote, "Alex Martelli") seems to me to turn arg 1 of display_quote into a constant string. Is this part of the point of currying, or an artifact of an approach to currying?

@Anon 2009-08-31 17:03:15

I think that is exactly the crux of the terminology issue. I, like numerous others out there, was using the term in a way where wiring in arguments was the whole point of currying. Others, I have learned thanks to your question ;-), would instead agree with "artifact of an approach to currying." Good to know this difference in usage exists out there.

@Eric M 2009-08-31 17:06:28

@Anon - well it is a quite new and still somewhat strange paradigm to me, so I certainly am not ready to state a personal preference! Knowing there is a difference in usage helps in rying understand the thing.

@ljs 2008-08-30 21:06:05

Currying is a transformation that can be applied to functions to allow them to take one less argument than previously.

For example, in F# you can define a function thus:-

let f x y z = x + y + z

Here function f takes parameters x, y and z and sums them together so:-

f 1 2 3

Returns 6.

From our definition we can can therefore define the curry function for f:-

let curry f = fun x -> f x

Where 'fun x -> f x' is a lambda function equivilent to x => f(x) in C#. This function inputs the function you wish to curry and returns a function which takes a single argument and returns the specified function with the first argument set to the input argument.

Using our previous example we can obtain a curry of f thus:-

let curryf = curry f

We can then do the following:-

let f1 = curryf 1

Which provides us with a function f1 which is equivilent to f1 y z = 1 + y + z. This means we can do the following:-

f1 2 3

Which returns 6.

This process is often confused with 'partial function application' which can be defined thus:-

let papply f x = f x

Though we can extend it to more than one parameter, i.e.:-

let papply2 f x y = f x y
let papply3 f x y z = f x y z

A partial application will take the function and parameter(s) and return a function that requires one or more less parameters, and as the previous two examples show is implemented directly in the standard F# function definition so we could achieve the previous result thus:-

let f1 = f 1
f1 2 3

Which will return a result of 6.

In conclusion:-

The difference between currying and partial function application is that:-

Currying takes a function and provides a new function accepting a single argument, and returning the specified function with its first argument set to that argument. This allows us to represent functions with multiple parameters as a series of single argument functions. Example:-

let f x y z = x + y + z
let curryf = curry f
let f1 = curryf 1
let f2 = curryf 2
f1 2 3
f2 1 3

Partial function application is more direct - it takes a function and one or more arguments and returns a function with the first n arguments set to the n arguments specified. Example:-

let f x y z = x + y + z
let f1 = f 1
let f2 = f 2
f1 2 3
f2 1 3

@cdmckay 2012-07-25 01:43:54

So methods in C# would need to be curried before they could be partially applied?

@Fuzzy Analysis 2014-09-13 22:36:42

"This allows us to represent functions with multiple parameters as a series of single argument functions" - perfect, that cleared it all up nicely for me. Thanks

@Vadim S. 2018-09-16 14:40:59

Here you can find a simple explanation of currying implementation in C#. In the comments, I have tried to show how currying can be useful:

public static class FuncExtensions {
    public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(this Func<T1, T2, TResult> func)
        return x1 => x2 => func(x1, x2);

var add = new Func<int, int, int>((x, y) => x + y).Curry();
var func = add(1);

//Obtaining the next parameter here, calling later the func with next parameter.
//Or you can prepare some base calculations at the previous step and then
//use the result of those calculations when calling the func multiple times 
//with different input parameters.

int result = func(1);

@Marcus Thornton 2018-06-21 06:49:22

Curry can simplify your code. This is one of the main reasons to use this. Currying is a process of converting a function that accepts n arguments into n functions that accept only one argument.

The principle is to pass the arguments of the passed function, using the closure (closure) property, to store them in another function and treat it as a return value, and these functions form a chain, and the final arguments are passed in to complete the operation.

The benefit of this is that it can simplify the processing of parameters by dealing with one parameter at a time, which can also improve the flexibility and readability of the program. This also makes the program more manageable. Also dividing the code into smaller pieces would make it reuse-friendly.

For example:

function curryMinus(x) 
  return function(y) 
    return x - y;

var minus5 = curryMinus(1);

I can also do...

var minus7 = curryMinus(7);

This is very great for making complex code neat and handling of unsynchronized methods etc.

@user3804449 2018-04-18 08:47:09

As all other answers currying helps to create partially applied functions. Javascript does not provide native support for automatic currying. So the examples provided above may not help in practical coding. There is some excellent example in livescript (Which essentially compiles to js)

times = (x, y) --> x * y
times 2, 3       #=> 6 (normal use works as expected)
double = times 2
double 5         #=> 10

In above example when you have given less no of arguments livescript generates new curried function for you (double)

@S2dent 2016-04-11 07:03:26

An example of currying would be when having functions you only know one of the parameters at the moment:

For example:

func aFunction(str: String) {
    let callback = callback(str) // signature now is `NSData -> ()`

func callback(str: String, data: NSData) {
    // Callback code

func performAsyncRequest(callback: NSData -> ()) {
    // Async code that will call callback with NSData as parameter

Here, since you don't know the second parameter for callback when sending it to performAsyncRequest(_:) you would have to create another lambda / closure to send that one to the function.

@nikk wong 2016-09-04 00:22:32

is func callback returning itself? It's being called @ callback(str) so let callback = callback(str), callback is just the return value of func callback

@S2dent 2016-09-05 21:49:33

no, func callback(_:data:) accepts two parameters, here I only give it one, the String, so it is waiting for the next one (NSData), this is why now let callback is another function waiting for data to be passed in

@Mario 2015-06-26 20:26:56

If you understand partial you're halfway there. The idea of partial is to preapply arguments to a function and give back a new function that wants only the remaining arguments. When this new function is called it includes the preloaded arguments along with whatever arguments were supplied to it.

In Clojure + is a function but to make things starkly clear:

(defn add [a b] (+ a b))

You may be aware that the inc function simply adds 1 to whatever number it's passed.

(inc 7) # => 8

Let's build it ourselves using partial:

(def inc (partial add 1))

Here we return another function that has 1 loaded into the first argument of add. As add takes two arguments the new inc function wants only the b argument -- not 2 arguments as before since 1 has already been partially applied. Thus partial is a tool from which to create new functions with default values presupplied. That is why in a functional language functions often order arguments from general to specific. This makes it easier to reuse such functions from which to construct other functions.

Now imagine if the language were smart enough to understand introspectively that add wanted two arguments. When we passed it one argument, rather than balking, what if the function partially applied the argument we passed it on our behalf understanding that we probably meant to provide the other argument later? We could then define inc without explicitly using partial.

(def inc (add 1)) #partial is implied

This is the way some languages behave. It is exceptionally useful when one wishes to compose functions into larger transformations. This would lead one to transducers.

@nazar_art 2014-05-04 09:03:57

A curried function is applied to multiple argument lists, instead of just one.

Here is a regular, non-curried function, which adds two Int parameters, x and y:

scala> def plainOldSum(x: Int, y: Int) = x + y
plainOldSum: (x: Int,y: Int)Int
scala> plainOldSum(1, 2)
res4: Int = 3

Here is similar function that’s curried. Instead of one list of two Int parameters, you apply this function to two lists of one Int parameter each:

scala> def curriedSum(x: Int)(y: Int) = x + y
curriedSum: (x: Int)(y: Int)Intscala> second(2)
res6: Int = 3
scala> curriedSum(1)(2)
res5: Int = 3

What’s happening here is that when you invoke curriedSum, you actually get two traditional function invocations back to back. The first function invocation takes a single Int parameter named x , and returns a function value for the second function. This second function takes the Int parameter y.

Here’s a function named first that does in spirit what the first traditional function invocation of curriedSum would do:

scala> def first(x: Int) = (y: Int) => x + y
first: (x: Int)(Int) => Int

Applying 1 to the first function—in other words, invoking the first function and passing in 1 —yields the second function:

scala> val second = first(1)
second: (Int) => Int = <function1>

Applying 2 to the second function yields the result:

scala> second(2)
res6: Int = 3

@Jon Harrop 2008-10-19 05:35:02

A curried function is a function of several arguments rewritten such that it accepts the first argument and returns a function that accepts the second argument and so on. This allows functions of several arguments to have some of their initial arguments partially applied.

@acarlon 2013-09-04 23:19:33

"This allows functions of several arguments to have some of their initial arguments partially applied." - why is that beneficial?

@Jon Harrop 2013-09-05 10:32:09

@acarlon Functions are often called repeatedly with one or more arguments the same. For example, if you want to map a function f over a list of lists xss you can do map (map f) xss.

@acarlon 2013-09-05 11:24:50

Thank you, that makes sense. I did a bit more reading and it has fallen into place.

@MindJuice 2015-10-28 05:48:18

I think this answer gets it right in a nice concise way. The "currying" is the process of taking the function of multiple arguments and converting it into a serious of functions that each take a single argument and return a function of a single argument, or in the case of the final function, return the actual result. This can either be done for you automatically by the language, or you can call a curry() function in other languages to generate the curried version. Note that calling a curried function with a parameter is not currying. The currying already happened.

@Shea Daniels 2009-08-30 02:22:54

Here's a concrete example:

Suppose you have a function that calculates the gravitational force acting on an object. If you don't know the formula, you can find it here. This function takes in the three necessary parameters as arguments.

Now, being on the earth, you only want to calculate forces for objects on this planet. In a functional language, you could pass in the mass of the earth to the function and then partially evaluate it. What you'd get back is another function that takes only two arguments and calculates the gravitational force of objects on earth. This is called currying.

@shuckster 2009-08-30 02:26:39

As a curiosity, the Prototype library for JavaScript offers a "curry" function that does pretty much exactly what you've explained here:

@Shea Daniels 2009-08-30 02:39:50

That's pretty cool. I did this example in Scheme a long time ago...

@Richard Ayotte 2012-12-12 16:53:27

@neontapir 2013-04-01 21:48:36

This sounds like partial application to me. My understanding is that if you apply currying, you can create functions with a single argument and compose them to form more complicated functions. Am I missing something?

@MindJuice 2015-10-28 05:31:28

@neontapir is correct. What Shea described is not currying. It is partial application. If a three-argument function is curried and you call it as f(1), what you get back is not a two-argument function. You get back a one-argument function that returns another one-argument function. A curried function can only ever be passed one argument. The curry function in PrototypeJS is also not currying. It's partial application.

@Will Ness 2018-08-22 13:29:43

no (to partial evaluation) and no (to currying). this is known as partial application. currying is needed to enable it.

@James Black 2009-08-30 02:19:59

I found this article, and the article it references, useful, to better understand currying:

As the others mentioned, it is just a way to have a one parameter function.

This is useful in that you don't have to assume how many parameters will be passed in, so you don't need a 2 parameter, 3 parameter and 4 parameter functions.

@Alex Martelli 2009-08-30 02:08:19

In an algebra of functions, dealing with functions that take multiple arguments (or equivalent one argument that's an N-tuple) is somewhat inelegant -- but, as Moses Schönfinkel (and, independently, Haskell Curry) proved, it's not needed: all you need are functions that take one argument.

So how do you deal with something you'd naturally express as, say, f(x,y)? Well, you take that as equivalent to f(x)(y) -- f(x), call it g, is a function, and you apply that function to y. In other words, you only have functions that take one argument -- but some of those functions return other functions (which ALSO take one argument;-).

As usual, wikipedia has a nice summary entry about this, with many useful pointers (probably including ones regarding your favorite languages;-) as well as slightly more rigorous mathematical treatment.

@Eric M 2009-08-30 21:50:42

I suppose similar comment to mine above - I have not seen that functional languages restrict functions to taking a single arg. Am I mistaken?

@Sam DeFabbia-Kane 2009-08-30 23:00:08

@hoohoo: Functional languages don't generally restrict functions to a single argument. However, on a lower, more mathematical level it's a lot easier to deal with functions that only take one argument. (In lambda calculus, for example, functions only take one argument at a time.)

@Eric M 2009-08-31 14:59:38

OK. Another questions then. Is the following a true statement? Lambda calculus can be used as a model of functional programming but functional programming is not necessarily applied lambda calculus.

@Alex Martelli 2009-08-31 15:20:08

As wikipedia pages note, most FP languages "embellish" or "augment" lambda calculus (e.g. with some constants and datatypes) rather than just "applying" it, but it's not that close. BTW, what gives you the impression that e.g. Haskell DOESN'T "restrict functions to taking a single arg"? It sure does, though that's irrelevant thanks to currying; e.g. div :: Integral a => a -> a -> a -- note those multiple arrows? "Map a to function mapping a to a" is one reading;-). You could use a (single) tuple argument for div &c, but that would be really anti-idiomatic in Haskell.

@Eric M 2009-08-31 17:03:32

@Alex - wrt Haskell & arg count, I have not spent a lot of time on Haskell, and that was all a few weeks ago. So it was an easy error to make.

Related Questions

Sponsored Content

36 Answered Questions

[SOLVED] What is dependency injection?

24 Answered Questions

[SOLVED] Does functional programming replace GoF design patterns?

45 Answered Questions

[SOLVED] What is a monad?

23 Answered Questions

21 Answered Questions

[SOLVED] What is reflection and why is it useful?

9 Answered Questions

[SOLVED] What is JSONP, and why was it created?

32 Answered Questions

[SOLVED] What exactly is RESTful programming?

  • 2009-03-22 14:45:39
  • hasen
  • 1633706 View
  • 3909 Score
  • 32 Answer
  • Tags:   http rest definition

27 Answered Questions

[SOLVED] What is tail recursion?

14 Answered Questions

18 Answered Questions

Sponsored Content