有没有办法在运行时找出函数有多少输入(参数,参数)?
说,我想:
(define (my-function unknown-function)
...
(number-of-necessary-arguments unknown-function)
...)
答案 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))