动态地找出函数有多少输入,Racket

时间:2011-09-08 01:12:42

标签: scheme racket arity

有没有办法在运行时找出函数有多少输入(参数,参数)?

说,我想:

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

1 个答案:

答案 0 :(得分:11)

您可以使用procedure-arity

(procedure-arity expt)                     ; => 2

请注意,当procedure-arity使用可变参数函数或case-lambda等时,结果会更复杂:

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