Questions tagged [currying]

Currying is the process of transforming a function of multiple arguments into a function of one argument that returns another function, which takes one fewer argument than the original one. Languages such as Haskell use this as the default argument application mechanism, as it makes certain programming techniques, such as partial application, much easier.

Currying is the process of transforming a function of multiple arguments into a function of one argument that returns another function, which takes one fewer argument than the original one. Languages such as use this as the default argument application mechanism, as it makes certain programming techniques, such as , much easier.

The technique originates in , and was independently discovered by Frege, Schönfinkel and Curry in the early 20th century.

Example of manual currying ()

Uncurried form

/* definition */
let add = (a, b) => a + b;

/* full application */
let x = add(2, 4);

/* partial application */
let add2 = add.bind(null, 2);
let y = add2(4);

Curried form

/* definition */
let add = a => b => a + b;

/* full application */
let x = add(2)(4);

/* partial application */
let add2 = add(2);
let y = add2(4);

Curried programming languages

1062 questions
9
votes
1 answer

C++11: lambda, currying

I have the following code. Can you explain to me how it works? template auto curry(Function func, Arguments... args) { return [=](auto... rest) { return func(args..., rest...); }; } int…
Cfon
  • 235
  • 2
  • 9
9
votes
4 answers

Is currying just a way to avoid inheritance?

So my understanding of currying (based on SO questions) is that it lets you partially set parameters of a function and return a "truncated" function as a result. If you have a big hairy function takes 10 parameters and looks like function (location,…
Alex Mcp
  • 19,037
  • 12
  • 60
  • 93
9
votes
4 answers

F# currying efficiency?

I have a function that looks as follows: let isInSet setElems normalize p = normalize p |> (Set.ofList setElems).Contains This function can be used to quickly check whether an element is semantically part of some set; for example, to check…
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
9
votes
1 answer

How does function application with the $ operator curry in Haskell?

I am learning haskell and am a little confused how the function application operator $ curry's. According to GHC the type of $ is *Main>:t ($) ($) :: (a->b) -> a -> b But I can type the following code *Main>map ($ 2) [(*2), (+2),…
user1840624
  • 159
  • 1
9
votes
7 answers

What are the benefits of currying?

I don't think I quite understand currying, since I'm unable to see any massive benefit it could provide. Perhaps someone could enlighten me with an example demonstrating why it is so useful. Does it truly have benefits and applications, or is it…
user997112
  • 29,025
  • 43
  • 182
  • 361
9
votes
1 answer

Ocaml's named parameters

Trying to understand Ocaml's mechanism for named parameters. I understand the basics, but the doc shows an example like this: # let f ~x ~y = x - y;; val f : x:int -> y:int -> int = # let x = 3 and y = 2 in f ~x ~y;; - : int = 1 What exactly…
scry
  • 1,237
  • 12
  • 29
9
votes
1 answer

What is a list of curried programming languages?

I just learned from another question that Haskell is called a curried programming language because it applies function currying by default. What are other languages that display this behavior?
8
votes
2 answers

C++ Function bind repeating arguments to curried function

I am trying to understand the concept of currying and calling a function which concats three strings but by passing only two strings and using the second argument twice. However when I do this, the second argument is not getting sent to the…
ganeshran
  • 3,512
  • 7
  • 41
  • 69
8
votes
1 answer

lambda calculus: passing two values to a single parameter without currying

I cannot understand why the following beta reduction is permitted in untyped lambda calculus: (λx.x y) (u v) -> ((u v) y) Specifically I cannot understand how one can pass two parameters u and v to a single parameter x in the λx.x part. To permit…
dendini
  • 3,842
  • 9
  • 37
  • 74
8
votes
3 answers

How is a partial application represented at runtime?

When I write something like map (1+) list in Haskell, what is the internal representation of (1+)? Since it is a partial application of (+), the argument 1 has to be saved somewhere, but I can't get my head around this. Can somebody give me a brief…
fuz
  • 88,405
  • 25
  • 200
  • 352
8
votes
3 answers

Rework for loop over STL container to use functional techniques

I have a std::vector of pointers Person objects, which have a member function std::string getName() const. Using STL algorithms I want to count all the Person objects in the vector where getName() returns "Chad". The behaviour simply iterating over…
8
votes
1 answer

Default parameters with currying

I can define a function as: def print(n:Int, s:String = "blah") {} print: (n: Int,s: String)Unit I can call it with: print(5) print(5, "testing") If I curry the above: def print2(n:Int)(s:String = "blah") {} print2: (n: Int)(s: String)Unit I…
ssanj
  • 2,169
  • 3
  • 19
  • 28
8
votes
1 answer

Perl 6 - Curried Function Hangs

So, I wanted to be able to write a function that will figure out all the ways that you could make change for a specific amount of money, using coins of different values. So, I wrote a function coin that tells you for a given amount, how many ways…
user6189164
  • 667
  • 3
  • 7
8
votes
4 answers

ES6 double arrow parameters (i.e. const update = x => y => { } )

What does double arrow parameters mean in the following code? const update = x => y => { // Do something with x and y } How is it different compared to the following? const update = (x, y) => { // Do something with x and y } Thanks!
His
  • 5,891
  • 15
  • 61
  • 82
8
votes
1 answer

In Scala, is it possible to "curry" type parameters of a def?

Let's suppose I have a def that takes multiple type parameters: def foo[A, B, C](b: B, c: C)(implicit ev: Writer[A]) However, the intended usage is that type parameters B and C should be inferred (based on the passed-in arguments). And the caller…
tksfz
  • 2,932
  • 1
  • 23
  • 25