SICP Exercise 3.51 showing delayed evaluation

Exercise 3.51.  In order to take a closer look at delayed evaluation, we will use the following procedure, which simply returns its argument after printing it:

(define (show x)
  (display-line x)
  x)


What does the interpreter print in response to evaluating each expression in the following sequence?59


(define x (stream-map show (stream-enumerate-interval 0 10)))
(stream-ref x 5)
(stream-ref x 7)


SOLUTION

The code and tests are here.

Note: I spent a lot of time tracing through the logic to see what really happens here. The delayed evaluation of items in streams combined with new streams being produced in procedure stream-map makes it really confusing. 

Also, note that in Racket, both the car part and the cdr part of the stream are delayed evaluations until the point of need.

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions