[plt-scheme] Currying functions?

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Feb 3 21:43:35 EST 2006

On Feb  3, Gregory Woodhouse wrote:
> 
> But is that even possible? Maybe, with delayed evaluation. I'll have
> to think about it.

If you always want a two-stage version, then you can use something
like this:

  (define (curry f . args)
    (lambda xs (apply f (append args xs))))

which is used to create the curried version.

  > ((curry list 1 2 3) 11 22 33)
  (1 2 3 11 22 33)

You can keep using `curry' instead of applications as long as you just
want to add more values:

  > ((curry (curry list 1 2 3) 11 22 33) 111 222 333)
  (1 2 3 11 22 33 111 222 333)

Alternatively, you can check the arity, and invoke the real function
as soon as you have enough arguments:

  (define ((currify-wrapper f stored-args) . args)
    (let ([args (append stored-args args)])
      (if (procedure-arity-includes? f (length args))
        (apply f args)
        (currify-wrapper f args))))
  (define (currify f)
    (currify-wrapper f '()))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.