开发者

Dynamically find out how many inputs a function has, Racket

开发者 https://www.devze.com 2023-04-03 04:25 出处:网络
Is there a way to find out at ru开发者_Go百科ntime, how many inputs (arguments, parameters) a function has?

Is there a way to find out at ru开发者_Go百科ntime, how many inputs (arguments, parameters) a function has?

Say, I want to:

(define (my-function unknown-function)
  ...
  (number-of-necessary-arguments unknown-function)
  ...)


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号