[英]How to apply a function in Scheme to a list of arguments returned by another function?
Suppose there are two functions, f and v. Assume further that v returns a list of length n and f expects exactly n arguments.假设有两个函数,f 和 v。进一步假设 v 返回一个长度为 n 的列表,并且 f 期望恰好 n arguments。 I am looking for the correct syntax in Scheme for applying f to the list returned by v.
我正在寻找将 f 应用于 v 返回的列表的正确语法。
If I use the syntax (f (v v-arguments))
then I get an error about f expectsing n arguments but receiving only one argument (which is the list returned by v).如果我使用语法
(f (v v-arguments))
,那么我会收到关于 f 期望 n arguments 但只接收一个参数(即 v 返回的列表)的错误。
If I use the syntax (f. (v v-arguments))
, then the problem is too many arguments passed to f.如果我使用语法
(f. (v v-arguments))
,那么问题是传递给 f 的 arguments 太多了。
The best I could do (for the case when f expects two arguments) is this:我能做的最好的事情(对于 f 需要两个参数的情况)是这样的:
(let ((output-of-v (v v-arguments)))
(f (car output-of-v) (cadr output-of-v)))
I am sure there must be a better way and I would be grateful for any advice!我相信一定有更好的方法,如果有任何建议,我将不胜感激!
It seems you're looking for apply :看来您正在寻找apply :
(apply f (v v-arguments))
As explained here , the apply
function applies a function to a list of arguments, effectively passing them as positional arguments to that function. As explained here , the
apply
function applies a function to a list of arguments, effectively passing them as positional arguments to that function.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.