方案,SICP,R5RS,为什么延迟不是特殊形式?
这是关于 SICP 的第 3.5 章,其中正在讨论流。这个想法是:
(cons-stream 1 (display 'hey))
不应该评估 cons-stream 的第二部分,因此它不应该打印“hey”。这确实发生了,我得到以下输出:
hey(1 . #
所以我的结论是延迟不是作为特殊形式实现的?或者我做错了什么?我使用以下实现:
(define (cons-stream a b)
(cons a (delay b)))
延迟是默认的 R5RS 实现。这是实施过程中的错误,还是我没有正确执行或理解它?
This is concerning chapter 3.5 from SICP, in which streams are being discussed. The idea is that:
(cons-stream 1 (display 'hey))
Should not evaluate the second part of the cons-stream, so it should not print “hey”. This does happen, I get the following output:
hey(1 . #< promise >)
So my conclusion is that delay is not implemented as a special form? Or am I doing something wrong? I use the following implementation:
(define (cons-stream a b)
(cons a (delay b)))
With delay being the default R5RS implementation. Is this a fault in the implementation, or am I not doing or understanding it right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实创建了一个promise,但是该promise是在您的
cons-stream
内创建的,这意味着为时已晚并且表达式已经被评估。试试这个:你会发现它评估得太早了。出于同样的原因,
当您的
cons-stream
是一个函数时, this: 和任何其他无限列表都将不起作用。所以问题是delay
是一种特殊形式,但您没有使用它的功能,因为您将cons-stream
定义为普通函数。如果您想让它也以相同的特殊方式运行,则必须将 cons-stream 定义为宏。例如:You do create a promise, but the promise is created inside your
cons-stream
, which means that it's too late and the expression was already evaluated. Try this:and you'll see that it's evaluated too early. For the same reason, this:
and any other infinite list won't work when your
cons-stream
is a function. So the thing is thatdelay
is a special form, but you're not using its feature since you definecons-stream
as a plain function. You have to definecons-stream
as a macro if you want to make it behave in the same special way too. For example: