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

我与影子孤独终老i 提交于 2019-12-03 12:55:35

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