EDIT: After solving the next exercise and looking at other solutions, it seems like the evaluations aren't actually being delayed, could it be some aspect of Racket's implementation which is preventing it from working?
master, the "delay" implementation that you have provided doesn't prevent the expression from being evaluated while you call the delay function.
While making the call (delay <exp>) : How do you convey to the interpreter that the exp should not be evaluated before calling the delay function.
The book mentions that delay is equivalent to the implementation(not equal) that you have provided as it requires a special syntax rule that prevents execution while calling. I guess that is why in the book they said they are equivalent but they never said they are equal. Remove your custom implementation of delay and use the language provided and you will see the expected behavior.
More specifically, if cons-stream can avoid evaluating (stream-map proc (stream-cdr s)) in the book implementation of stream-map, then all will work fine.
In MIT/GNU Scheme, redefining delay won't make cons-stream not work (maybe cons-stream is not based on delay in MIT/GNU Scheme implementation). Actually merely making cons-stream not one special form will cause the above (define x ...) behavior but in MIT/GNU Scheme it outputs from 10 to 0 maybe due to calling recursion first before getting (proc (stream-car s)).
---
Actually master's problem is caused by redefining cons-stream, delay as one lambda func which when called will also implicitly evaluate its arg. But if we manually substitute cons-stream (similar for delay) with its definition in each caller as https://stackoverflow.com/a/30561923/21294350 does, then all work fine. But that is very inconvenient.
master
I get a different result. On Racket, with all of the procedures implemented exactly as specified in the book, I get:
The only thing which could possibly account for this discrepancy is a mistake in my implementation of delay, but it looks fine to me?
(define delay (lambda (proc) (memo-proc (lambda () proc))))
Any idea why I get different results?
EDIT: After solving the next exercise and looking at other solutions, it seems like the evaluations aren't actually being delayed, could it be some aspect of Racket's implementation which is preventing it from working?
madhusudann
master, the "delay" implementation that you have provided doesn't prevent the expression from being evaluated while you call the delay function. While making the call (delay <exp>) : How do you convey to the interpreter that the exp should not be evaluated before calling the delay function. The book mentions that delay is equivalent to the implementation(not equal) that you have provided as it requires a special syntax rule that prevents execution while calling. I guess that is why in the book they said they are equivalent but they never said they are equal. Remove your custom implementation of delay and use the language provided and you will see the expected behavior.
LisScheSic
More specifically, if cons-stream can avoid evaluating (stream-map proc (stream-cdr s)) in the book implementation of stream-map, then all will work fine.
In MIT/GNU Scheme, redefining delay won't make cons-stream not work (maybe cons-stream is not based on delay in MIT/GNU Scheme implementation). Actually merely making cons-stream not one special form will cause the above (define x ...) behavior but in MIT/GNU Scheme it outputs from 10 to 0 maybe due to calling recursion first before getting (proc (stream-car s)).
---
Actually master's problem is caused by redefining cons-stream, delay as one lambda func which when called will also implicitly evaluate its arg. But if we manually substitute cons-stream (similar for delay) with its definition in each caller as https://stackoverflow.com/a/30561923/21294350 does, then all work fine. But that is very inconvenient.
IMHO this workaround is just how macro https://stackoverflow.com/a/79054260/21294350 works.