Trouble implementing streams with Racket

I am working my way through the Wizard Book Structure and Interpretation of Programming in my free evenings. All was going well, until I tried to implement streams in Chapter Three. I am using (Dr)Racket, not Scheme, so this might be my problem.

The crux of the problem is that when I try to create streams I get a memory issue: "interactions disabled; out of memory". This suggests to me that there is something wrong with the way I am using delay.

Thanks in advance for any advice :)

I have tried the code suggested in the text:

#lang racket

(define (cons-stream a b)
  (cons a (delay b)))

(define (car-stream s)
  (car s))

(define (cdr-stream s)
  (force (cdr s)))

where I am calling Racket's delay and force. I have also tried implementing delay and force myself following the text, but I come across the same memory error.

To test that this is all working I tried writing an infinite stream of 1s:

(define (ones) (cons-stream 1 (ones)))

and a very large interval:

(define (interval low high)
  (if (> low high)
      (cons-stream low
                   (interval (+ low 1) high))))

(define (first-integers n)
  (interval 0 n))

(define test-interval
  (first-integers 10000000000000000000000000))

To my understanding of the text, neither of these should cause problems because we only generate the streams when required. However, something I've done causes a memory issue and so I think the code tries to generate the entire stream!


  • We can not define

    (define (cons-stream a b)
      (cons a (delay b)))

    because define creates a function cons-stream, so on every function call all arguments will be evaluated before the call is made. Which defeats the whole purpose -- b will be already evaluated.

    See also How is delay implemented?.