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?
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: