是否有与 lisp 的 apply 函数等价的功能?
看看perform 和performWithArgList 方法。
退后一步,您可以在 Io 中以几种不同的方式复制 Lisp 的 FUNCALL:
1 - 传递一个函数,即。块():
plotf := method (fn, min, max, step,
for (i, min, max, step,
fn call(i) roundDown repeat(write("*"))
writeln
)
)
plotf( block(n, n exp), 0, 4, 1/2 )
2 - 传递消息:
plotm := method (msg, min, max, step,
for (i, min, max, step,
i doMessage(msg) roundDown repeat(write("*"))
writeln
)
)
plotm( message(exp), 0, 4, 1/2 )
3 - 将函数名作为字符串传递:
plots := method (str, min, max, step,
for (i, min, max, step,
i perform(str) roundDown repeat(write("*"))
writeln
)
)
plots( "exp", 0, 4, 1/2 )
因此,您可以从所有这些中创建一个 Lisp APPLY,如下所示:
apply := method (
args := call message argsEvaluatedIn(call sender)
fn := args removeFirst
performWithArgList( fn, args )
)
apply( "plotf", block(n, n exp), 0, 4, 1/2 )
apply( "plotm", message(exp), 0, 4, 1/2 )
apply( "plots", "exp", 0, 4, 1/2 )