免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: win_hate
打印 上一主题 下一主题

David Madore 的 Scheme 迷题解释 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-09-06 12:53 |显示全部楼层
原帖由 swordfish.cn 于 2008-9-6 11:32 发表
另,我用

(define (call/cc) (call-with-current-continuation))

也没有报错,能否解释一下为何?



你这样做是定义了无参函数 call/cc, 执行时调用 call-with-current-continuation

定义的时候不会出错,但运行的时候应该会出错。


  1. (define (id x) x)

  2. (define (call/cc) (call-with-current-continuation))
复制代码

guile> (load "2.scm")
guile> (call-with-current-continuation id)
#<continuation 882 @ 69be70>
guile> (call/cc id)

Backtrace:
In current input:
   3: 0* [call/cc #<procedure id (x)>]

<unnamed port>:3:1: In procedure call/cc in expression (call/cc id):
<unnamed port>:3:1: Wrong number of arguments to #<procedure call/cc ()>
ABORT: (wrong-number-of-args)
guile> (call/cc)

Backtrace:
In current input:
   4: 0* [call/cc]
In 2.scm:
   3: 1  [call-with-current-continuation]

2.scm:3:19: In procedure call-with-current-continuation in expression (call-with-current-continuation):
2.scm:3:19: Wrong number of arguments to #<procedure call-with-current-continuation (proc)>
ABORT: (wrong-number-of-args)
guile>

论坛徽章:
0
12 [报告]
发表于 2008-09-06 12:59 |显示全部楼层
由于你定义的 call/cc 是无参的,所以不能象一般 call/cc 那样调用,(call/cc id) 会出错。

但如果按无参方式调用,则 call/cc 转而执行 (call-with-current-continuation)。但 call-with-current-continuation 需要一个参数,所以仍然会报错。

我下了个 windows 上的 MIT-Scheme,它带了一个类似 emacs 的编辑环境。弄了半天,没明白如何让它执行代码。

现在正编译一个 MIT-Scheme 的 portable 版本。

[ 本帖最后由 win_hate 于 2008-9-6 13:03 编辑 ]

论坛徽章:
0
13 [报告]
发表于 2008-09-06 17:08 |显示全部楼层
ctrl-x ctrl+e 能执行。(汗,我早上试过这个方法,但运行不了,不知当时那里搞错了。)

我曾经怀疑是求值次序的问题,但你给我的序列是 OO-OO-,是先有 O 的,又不象次序有问题。

现在我自己运行了一下,输出其实是

-OO-OO-OO-OO-.......

这表明 MIT schmem 是先对 (g (call/cc id)) 求的值,然后才 (f (call/cc id))

[ 本帖最后由 win_hate 于 2008-9-6 17:22 编辑 ]

论坛徽章:
0
14 [报告]
发表于 2008-09-06 17:22 |显示全部楼层
MIT-Scheme 执行前述代码的分析:

( (f (call/cc id)) (g (call/cc id)) )

首先后面的 call/cc 产生一个 continuation c0,然后前面的 call/cc 产生一个 continuation c1,  ;; -O

现在是 (c1 c0) , 所以 c1 带值 c0 返回,因为求值次序,所以后面的 call/cc 不会被执行,当前求值表达式为 (c0 c0) -> c0, ;; -OO

此时回到开始的 c0, 前面的 call/cc 造出 c2,如是循环下去,就得到了

-OO-OO-OO-OO......


这个求值次序下,分析起来还要容易一些。

为便于实现,scheme 没有规定求值次序。

[ 本帖最后由 win_hate 于 2008-9-6 17:23 编辑 ]

论坛徽章:
0
15 [报告]
发表于 2008-09-06 17:31 |显示全部楼层
不客气。共同学习,一起进步。

论坛徽章:
0
16 [报告]
发表于 2008-09-10 10:36 |显示全部楼层
原帖由 Tiger_cn 于 2008-9-10 00:34 发表
我放在DrScheme里运行了 结果是
O------------------------------------------------------------------------------------------------------------------------------------------------------------------- ...


Lazy 会尽量推迟求值,所以 (f (call/cc id)) 的求值次序是这样的:

f
(display "O")
(call/cc id)           ;; make continuation c0 here
...

所以通过 continuation 返回时,始终在 (display "O") 语句之后。O 仅会输出一次。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP