繁体   English   中英

方案延续定义中的一个棘手问题

[英]A tricky question in scheme continuation definition

R5rs 说

延续代表计算的整个(默认)未来”。

所以基本上在以下代码中:

(define x (call/cc (lambda (c) c)))
(display "hello\n")
(display "world\n")
(x 4)
(display x)

我尝试了几种实现,它们都输出

hello
world
4

在此示例中, call/cc 捕获的延续似乎仅限制了第一个顶级表达式的范围。 喜欢(define x ?)

我虽然基于 r5rs,当(x 4)执行时,执行会跳回到开始的定义形式并完成分配。 然后它将继续运行随后的两个显示表达式并运行(x 4) ,这将报告错误,因为 x 将不再是一个过程。

你的期望是正确的。 问题是 REPL 分别执行每个表达式,就好像您在每个表达式之间按了 ENTER 一样。 如果将它们包装在begin中,它会按预期工作。

$ scheme
Chez Scheme Version 9.5.8
Copyright 1984-2022 Cisco Systems, Inc.

> (begin
   (define x (call/cc (lambda (c) c)))
   (display "hello\n")
   (display "world\n")
   (x 4)
   (display x))
hello
world
hello
world
Exception: attempt to apply non-procedure 4
Type (debug) to enter the debugger.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM