[plt-scheme] What is the big deal about the currying of functions?

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu May 24 18:09:14 EDT 2007

Uhh.. you don't need macros to curry functions!

A curried function is a function that accepts two (or more) arguments,
but does it in a strange way. That is, it is really a function that
accepts one argument but they returns a new function that accepts the
second argument.

This is a canonical example:

 ;; two-arg-adder : number number -> number
 ;; uncurried version
 (define two-arg-adder (lambda (x y) (+ x y)))

 ;; curried-adder : number -> (number -> number)
 ;; curried version
 (define curried-adder (lambda (x) (lambda (y) (+ x y))))

To call them, you'd write this:

   (two-arg-adder 1 2) ; => 3
   ((curried-adder 1) 2) ; => 3

The advantage of the curried version is that you don't have to supply
all the arguments at once. Eg:

  (define add3 (curried-adder 3))

  (add3 4) ; => 7
  (add3 11) ; => 14

This can be useful in conjunction with Scheme's "loop"s, like map:

  (map (curried-adder 4) (list 1 2 3))
  ; => (list 5 6 7)

Also, you can use the define syntax to define the two functions above:

  (define ((curried-adder x) y) (+ x y))

This definition is semantically identical to the one above.

In some languages (eg SML) it is impossible to write a two argument
function. All functions take a single argument, and they use currying
to supply multiple arguments. Maybe that's where the uninformed macro
comment comes from. I don't know.


On 5/24/07, Grant Rettke <grettke at acm.org> wrote:
> I hear folks talk about how great is a language when it lets you curry
> functions, and that Scheme can do it too if you use macros.
> In your minds what is, and what is the value of, function currying?
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.