What Would Scheme Display?

As you work through these problems, remember that streams have two important components:

  • Lazy evaluation – so the remainder of the stream isn’t computed until explicitly requested.
  • Memoization – so anything we compute won’t be recomputed.

The examples here stretch these concepts to the limit. In most practical use cases, you may find you rarely need to redefine functions that compute the remainder of the stream.

Use Ok to unlock the following "What would Scheme print?" questions:

$ python ok -q wwsd -u
scm> (define (has-even? s)
       (cond ((null? s) #f)
             ((even? (car s)) #t)
             (else (has-even? (cdr-stream s)))))
has-even?
scm> (define (f x) (* 3 x))
f
scm> (define nums (cons-stream 1 (cons-stream (f 3) (cons-stream (f 5) nil))))
nums
scm> nums

scm> (cdr nums)

scm> (cdr-stream nums)

scm> nums

scm> (define (f x) (* 2 x))
f
scm> (cdr-stream nums)

scm> (cdr-stream (cdr-stream nums))

scm> (has-even? nums)