【发布时间】:2019-08-16 02:07:36
【问题描述】:
我还在学习球拍。
我必须调用一个未知函数。该函数及其参数在以下列表中:
(define l1 '((function-name parameter1)
(function-name parameter3)))
要运行该功能,我正在做:
(first (car l1)) (second (car l1)) another-parameter
但我得到了错误:
application: not a procedure;
expected a procedure that can be applied to arguments
given: 'function-name
arguments...:
如何运行 function-name?
更新:
我试过 Óscar 的回答:
(eval (first (car l1)) (second (car l1)) another-parameter)
我得到了错误:
eval: arity mismatch;
the expected number of arguments does not match the given number
given: 3
arguments...:
我也试过了:
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
(eval (first (car l1)) (second (car l1)) another-parameter ns)
我得到了同样的错误:
eval: arity mismatch;
the expected number of arguments does not match the given number
given: 4
arguments...:
然后,我尝试了这个:
(eval (list (first (car l1)) (second (car l1)) another-parameter))
我得到了错误:
function-name: unbound identifier;
also, no #%app syntax transformer is bound in: function-name
最后,我试过了:
(eval (list (first (car l1)) (second (car l1)) another-parameter) ns)
我收到来自function-name 的内部错误。但是这个功能,效果很好。
function-name 至少可以是三个(或更多)函数,这就是我之前没有放在这里的原因。它们都将有两个列表作为参数,它们将返回#t 或#f。
其中一个,然后一个正在测试的是:
(define match (lambda (list1 list2) ...))
显然,list1 和 list2 是列表。
更新 2:
我已经尝试过 Óscar 的 Minimal、Complete 和可验证的示例,并且它有效。但是,我已经修改为用于我的工作,它不起作用。看:
(define function-name
(lambda (list1 list2)
(append list1 list2)))
(define parameter1 '(1 2))
(define parameter3 '(3 4))
(define another-parameter '(5 6))
(define l1 '((function-name parameter1)
(function-name parameter3)))
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
(define another-function
(lambda (l1 the-parameter)
(cond
[(eval (list (first (car l1)) (second (car l1)) 'the-parameter) ns) l1])
)
)
(another-function l1 another-parameter)
我创建了another-function,但由于参数'the-parameter 而失败。它抱怨说:
the-parameter: undefined;
cannot reference an identifier before its definition
所以问题是当我使用函数的参数作为eval 函数的参数时。
【问题讨论】:
标签: list functional-programming scheme racket