Scheme, SICP, R5RS, why is delay not a special form?

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

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?

回答1:

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:

(define (foo x)   (display "foo: ") (write x) (newline)   x)  (cons-stream 1 (foo 2)) 

and you'll see that it's evaluated too early. For the same reason, this:

(define ones (cons-stream 1 ones)) 

and any other infinite list won't work when your cons-stream is a function. So the thing is that delay is a special form, but you're not using its feature since you define cons-stream as a plain function. You have to define cons-stream as a macro if you want to make it behave in the same special way too. For example:

(define-syntax cons-stream   (syntax-rules ()     [(cons-stream x y) (cons x (delay y))])) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!