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:
(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))]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With