[Python-Dev] PEP 309, function currying

Gareth McCaughan gmccaughan at synaptics-uk.com
Mon Feb 23 11:58:23 EST 2004


On Monday 2004-02-23 16:06, Andrew Koenig wrote:

> 	Function currying is the process of transforming a function that
> 	expects all of its arguments at once into a delayed function
> 	(called a curried function), that expects only its first argument.
> 	If the first argument is the only argument, currying is the
> 	identity function.  Otherwise, the delayed function, itself also
> 	curried, expects the first of the remaining arguments and yields
> 	either the result (if there is only one remaining argument) or
> 	a similarly delayed, curried function that deals with the second
> 	and subsequent remaining arguments.

I think it's better to define partical application and
currying together, as follows.

    Partial application is a way of transforming a function by
    specifying values for some of its arguments.

    If f is a function of n arguments and 0 <= m <= n, then the
    partial application of f to y1,...,ym is a function f1
    of n-m arguments such that

        f1(x(m+1),...,xn) = f(y1,...,ym, x(m+1),...,xn).

    Currying is a way of transforming a function so that instead
    of accepting all its arguments at once it accepts just the first,
    returning a function which accepts just the second, returning
    a function which accepts just the third, and so on. Formally,
    denote the partial application of f to y1,...,ym by f[y1,...,ym];
    then currying is the function transformer C such that

      - when f is a function of no arguments, C(f) = f();
      - when f is a function of at least one argument,
        C(f) is the 1-argument function taking y1 to C(f[y1]).

    Note that currying a no-argument function yields not a
    function but a constant; currying a 1-argument function
    yields the same function.

    It follows that C(f)(x1)(x2)...(xn) = f(x1,x2,...,xn).

In a context where the definition of partial application isn't
useful, this can be shortened to

    Currying is a way of transforming a function so that instead
    of accepting all its arguments at once it accepts just the first,
    returning a function which accepts just the second, returning
    a function which accepts just the third, and so on. Formally,
    given a function of at least one argument and a value y1, denote
    by f[y1] the function that takes (x2,...,xn) to f(y1,x2,...,xn).
    Then currying is the function transformer C such that

      - when f is a function of no arguments, C(f) = f();
      - when f is a function of at least one argument,
        C(f) is the 1-argument function taking y1 to C(f[y1]).

    It follows that C(f)(x1)(x2)...(xn) = f(x1,x2,...,xn).

    Note that currying a no-argument function yields not a
    function but a constant; currying a 1-argument function
    yields the same function; currying any function with at
    least one argument yields a function that takes exactly
    one argument.

I think it's elegant to start the definition at 0-argument
functions, but obviously you can alternatively define currying
only for functions of at least one argument, in which case
you have the advantage that it always yields a function :-).

-- 
g





More information about the Python-Dev mailing list