I have a firm understanding of how lazy evaluation and streams work.
However I'm having some trouble simply following the book at this point. I don't really undrestand what it is trying to tell me about delay
, and here is why.
At page 321 it reads
Cons-stream
is a special form defined so that(cons-stream 〈a〉 〈b〉)
is equivalent to
(cons 〈a〉 (delay 〈b〉))
and a related footnote
[…] If
cons-stream
were a procedure, then […] evaluating(cons-stream 〈a〉 〈b〉)
would automatically cause〈b〉
to be evaluated, which is precisely what we do not want to happen. For the same reason,delay
must be a special form […]
So I conclude that I can't implement cons-stream
nor delay
, at least not with the tools I have been given at this stage in the book, so I don't understand what the quote above is trying to tell me, beside "it's kinda this, but not quite".
Later, though, there's a "section" of the book titled
Implementing delay
and force
Which says something similar to before:
Delay
can be a special form such that(delay 〈exp〉)
is syntactic sugar for
(lambda () 〈exp〉)
But again, how do I define delay
if I don't know how to define a special form?
Paradoxically, immediately after one finds that
This implementation suffices for
delay
andforce
to work as advertised, but […]
(and the "but" has nothing to do with special forms but just with performance), but how can it work if it's not a special form, defined like this?
And even if I wanted to assume that delay
is already defined in whatever Scheme interpreter I'm using (which is the case for this online compiler), I would still be unable to define a special form that makes use of it, such as cons-stream
.
Bottom line, I don't know how to write anything for making the exercises.
Eventually, I found this answer showing how to define a special form, but the point is that the define-syntax
used therein is not even mentioned in the whole book.
So how was I supposed to do the exercises?