Dynamically find out how many inputs a function has, Racket
Asked Answered
S

1

6

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)
  ...)
Scalenus answered 8/9, 2011 at 1:12 Comment(1)
Don't you need "apply" actually?Ilse
S
12

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))
Sportsmanship answered 8/9, 2011 at 1:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.