免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3706 | 回复: 3

( (lambda x x) 2 3 4 5) [复制链接]

论坛徽章:
0
发表于 2009-01-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) ??

论坛徽章:
0
发表于 2009-01-25 22:40 |显示全部楼层
可以参考 “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 编辑 ]

论坛徽章:
0
发表于 2009-01-26 00:04 |显示全部楼层

回复 #1 pigjj 的帖子

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

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

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

评分

参与人数 1可用积分 +10 收起 理由
win_hate + 10 新春首贴,送红包。

查看全部评分

论坛徽章:
0
发表于 2009-02-27 01:01 |显示全部楼层
原帖由 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)就不会引起混淆了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP