On Fri, 2009-09-04 at 20:37 -0500, Eduardo Cavazos wrote:
> Hello,
> 
> Let's simulate 'procedure-arity' functionality for a few procedures:
> 
> (define (procedure-arity f)
>    (cdr (assq f (list (cons vector-ref  2)
>                       (cons vector-set! 3)
>                       (cons eq?         2)))))

(It would be better to create that a-list only once).

> Here's a basic curry macro:
> 
> (define-syntax curry
>    (syntax-rules ()
>      ((curry (f x)) f)
>      ((curry (f x y ...))
>       (lambda (x)
>         (curry ((lambda (y ...)
>                   (f x y ...))
>                 y ...))) )))
> 
> You have to specify the arity of the thing you're currying. For example:
> 
>      (curry (vector-set! x y z))
> 
> will give you a curried 'vector-set!'.
> 
> Below is some pseudo-code for a "smart-curry" which uses 
> procedure-arity. Is something like it possible?
> 
> (define-syntax smart-curry
>    (lambda (stx)
>      (syntax-case stx ()
>        ((smart-curry f)
> 
>         (with-syntax (((x ...) (generate-temporaries
>                                 (iota
>                                  (procedure-arity f)))))
>           (syntax (curry (f x ...))))))))
> 
> I guess it comes down to, can a macro "eval" that 'f' so that it's a 
> procedure which can then be passed to 'procedure-arity'?

Not really.  You could syntax->datum it and use eval on that in a
guessed environment, but you're totally guessing the environment, which
is very not good.

> Or is there another approach to something like this?

You could use free-identifier=?, but the f syntax must be an identifier.
(And all the procedures you're interested in must be bound in the
environment of the definition of procedure-arity, but that's the same as
your run-time version.).

  (define (procedure-arity f)
    (cdr (assp (lambda (x) (free-identifier=? f x))
               (list (cons (syntax vector-ref)  2)
                     (cons (syntax vector-set!) 3)
                     (cons (syntax eq?)         2)))))

> If stuff like this *is* possible, then I think it's makes a case for 
> 'procedure-arity'.

What if the procedure has multiple arities, e.g. a case-lambda or
(lambda args ---).  What would smart-curry do?  If you only use it with
single arity procedures, then this isn't a problem.

-- 
: Derick
----------------------------------------------------------------

Reply via email to