Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically find out how many inputs a function has, Racket

Is there a way to find out at runtime, how many inputs (arguments, parameters) a function has?

Say, I want to:

(define (my-function unknown-function)
  ...
  (number-of-necessary-arguments unknown-function)
  ...)
like image 298
Vladimir Keleshev Avatar asked Sep 08 '11 01:09

Vladimir Keleshev


1 Answers

You can use procedure-arity.

(procedure-arity expt)                     ; => 2

Note that when using procedure-arity with variadic functions or case-lambda or the like, the results are more complicated:

(procedure-arity apply)                    ; => (arity-at-least 2)
(procedure-arity (case-lambda
                  ((x) x)
                  ((x y z) z)
                  ((a b c d e f . g) g)))  ; => `(1 3 ,(arity-at-least 6))
like image 87
Chris Jester-Young Avatar answered Sep 19 '22 12:09

Chris Jester-Young