views:

1609

answers:

4
+29  Q: 

What is 'Currying'?

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!)

+18  A: 

Currying is a technique to pass multiple arguments to a function when the language only allows one argument and one return value. Suppose you have a function f which takes arguments a and b. You call f with argument a and instead of doing anything, it returns a function which takes a second argument. You pass the second argument to that function, and then you get your result.

For example, in OCaml:

let add a b = a + b

This function would add two integers. You would call it like this:

add 1 2

If you just call it with one argument:

add 1

the result is a function that adds 1 to its argument. This is also called partial application.

Reference: Wikipedia on Currying

Jay Conrod
Currying isn't just useful when the _language_ only allows one argument. It's useful any time a function has too many arguments to be useful in your current context, and you know that you can fill in some of them reliably all the time.
Chris Ammerman
-1: Currying != Partial Application
trinithis
@Ben @Jay Conrod @trinithis Now discussed in another SO question: http://stackoverflow.com/questions/218025/what-is-the-difference-between-currying-and-partial-application
Lord Torgamus
+17  A: 

Currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments. Here's an example in Scheme

(define (add a b)
  (+ 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:

(define (add a)
  (lambda (b)
    (+ 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.

((add 3) 4)

(define add3 (add 3))

(add3 4)

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.

Kyle Cronin
+10  A: 

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
etc.

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
6
f2 1 3
6

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
6
f2 1 3
6
kronoz
A: 

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.

Jon Harrop