是否可以仅使用可用的函数名称来调用Scheme函数,例如列表中的字符串?
(define (somefunc x y)
(+ (* 2 (expt x 2)) (* 3 y) 1))
(define func-names (list "somefunc"))
然后使用(car func-names)
调用somefunc。
答案 0 :(得分:13)
在许多Scheme实现中,您可以使用eval
函数:
((eval (string->symbol (car func-names))) arg1 arg2 ...)
但是,你通常不想那样做。如果可能,将函数本身放入列表并调用它们:
(define funcs (list somefunc ...))
;; Then:
((car funcs) arg1 arg2 ...)
正如评论者指出的那样,如果您确实想要将字符串映射到函数,则需要手动执行此操作。由于函数是与任何其他函数一样的对象,因此您可以简单地为此目的构建字典,例如关联列表或哈希表。例如:
(define (f1 x y)
(+ (* 2 (expt x 2)) (* 3 y) 1))
(define (f2 x y)
(+ (* x y) 1))
(define named-functions
(list (cons "one" f1)
(cons "two" f2)
(cons "three" (lambda (x y) (/ (f1 x y) (f2 x y))))
(cons "plus" +)))
(define (name->function name)
(let ((p (assoc name named-functions)))
(if p
(cdr p)
(error "Function not found"))))
;; Use it like this:
((name->function "three") 4 5)