[plt-scheme] Currying functions?

From: Gregory Woodhouse (gregory.woodhouse at sbcglobal.net)
Date: Fri Feb 3 21:29:25 EST 2006

On Feb 3, 2006, at 5:56 PM, Danny Yoo wrote:

> Maybe something like this would work for you:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
> ;
> (module currify mzscheme
>   (provide currify)
>
>   (define (currify f)
>     (let ((N (procedure-arity f)))
>       (letrec
>           ((grab-rest-args
>             (lambda (n reversed-args-so-far)
>               (if (= n N)
>                   (apply f (reverse reversed-args-so-far))
>                   (lambda (x)
>                     (grab-rest-args (add1 n)
>                                     (cons x reversed-args-so- 
> far)))))))
>         (cond
>          ((and (number? N) (= N 0)) f)
>          ((number? N)
>           (lambda (first-arg)
>             (grab-rest-args 1 (list first-arg))))
>          (else
>           (error 'currify
>                  "Don't know how to handle procedure arity ~s"  
> N)))))))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
> ;

Thanks! I'm going to save that. The problem, of course, is that it  
can't be used curry functions taking a variable number of arguments:

 > (require currify)
 > (define f (lambda (x . y) x))
 > (define g (currify f))
. currify: Don't know how to handle procedure arity #<struct:arity-at- 
least>
 >

But is that even possible? Maybe, with delayed evaluation. I'll have  
to think about it.


===
Gregory Woodhouse
gregory.woodhouse at sbcglobal.net

"Education is a progressive discovery
of our own ignorance."
--Will Durant





Posted on the users mailing list.