作为编译并返回参数列表的列表运算符的定义,我在 this tutorial 找到了此方案代码。
(define list
(lambda args args))
但是,这与我所知道的通常的 lambda 语法不匹配。我的意思是,lambda 应该将参数放在括号等中。请解释它是如何工作的。
最佳答案
虽然 lambda 中参数的一般形式似乎总是包含括号,但这并不是必需的。如果看一下this section在 The Scheme Programming Language 4e ,它说:
The general form of lambda is a bit more complicated than the form we saw earlier, in that the formal parameter specification, (var ...), need not be a proper list, or indeed even a list at all. The formal parameter specification can be in any of the following three forms:
- a proper list of variables, (var1 ... varn), such as we have already seen,
- a single variable, varn, or
- an improper list of variables, (var1 ... varn . varr).
In the first case, exactly n actual parameters must be supplied, and each variable is bound to the corresponding actual parameter. In the second, any number of actual parameters is valid; all of the actual parameters are put into a single list and the single variable is bound to this list.
粗体部分(粗体是我设置的),是与你的问题非常相关的部分。正如您的教程所说,它是一个转换为列表的可变参数参数。
关于lambda - 这个方案代码是什么意思? (使用 lambda 定义 'list' 运算符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32577660/