6

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

说,我想:

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

1 回答 1

12

您可以使用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))
于 2011-09-08T01:41:05.667 回答