( (lambda x x) 2 3 4 5)

发布于 2022-08-09 16:43:40 字数 336 浏览 15 评论 3

scheme
lambda 的syntax  是: (lambda  (formals) (body) )

( (lambda x x) 2 3 4 5)  什么意思啊?
这里 第一个x 是 形参? 第二个x 是 body ?  2 3 4 5  其实是一个 传递给它的 实参列表 ' (2 3 4 5) ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

这样的小城市 2022-08-20 17:42:31

原帖由 pigjj 于 2009-1-25 22:23 发表
scheme
lambda 的syntax  是: (lambda  (formals) (body) )

( (lambda x x) 2 3 4 5)  什么意思啊?
这里 第一个x 是 形参? 第二个x 是 body ?  2 3 4 5  其实是一个 传递给它的 实参列表 ' (2 3 4 5 ...

你理解得很正确,第一个就是形参,第二个是body。
如果你不敢确认后面的2 3 4 5不是list,那分开调用可以看得清楚些:
(define fun
(lambda x
x))    ;什么都不做,直接返回传入值
执行(fun 2 3 4 5),结果
'(2 3 4 5)

(lambda x x)这样的写法容易引起混乱,比较规矩的写法还是把参数列表括起来,这样:
(lambda (x) x)就不会引起混淆了。

枕头说它不想醒 2022-08-20 16:32:08

(lambda x x) 表示:
这是个匿名函数, 参数列表为x, 函数体只有一个表达式, 就是返回参数列表x.
(lambda (x) x)表示:
这个函数的参数列表是(x), 这里x表示它的第一个参数.

无论如何, lambda表达式的第二个元素是它的参数列表.

再看
(lambda (x . y) y) 表示:
这个函数的参数列表是(x . y), x是第一个参数, y表示余下参数组成的列表.

栀子花开つ 2022-08-20 11:40:56

可以参考 “The Scheme Programming Language" 的 2.5 [点这里]

相关的是这一段:

As mentioned above, the general form for 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, varr, 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. The third case is a hybrid of the first two cases. At least n actual parameters must be supplied. The variables var1 ... varn are bound to the corresponding actual parameters, and the variable varr is bound to a list containing the remaining actual parameters. In the second and third cases, varr is sometimes referred to as a "rest" parameter because it holds the rest of the actual parameters beyond those that are individually named.

[ 本帖最后由 win_hate 于 2009-1-25 22:42 编辑 ]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文