0

I was reading SICP chapter 3 and thought of this (consider it a variation of the procedure integers that creates a stream of integers): how do you create a stream of two alternating values? For example you create this:

1 0 1 0 1 0 1 0 ...

and you can change the step to 2 (or more) and make it look like

1 1 0 0 1 1 0 0 1 1 ...

1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...

1 Answers1

-1
(define (make-alternating-values n)
  (define (iter i)
    (cons-stream 
      (if (> n 0)
        1
        0)
      (if (= i (- 1 n))
        (iter n)
        (iter (- i 1)))))
  (iter n))

(make-alternating-values 1)
; 1 0 1 0 1 0 1 0 ...
(make-alternating-values 2)
; 1 1 0 0 1 1 0 0 1 1 ...
(make-alternating-values 3)
; 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...
Jiacai Liu
  • 2,623
  • 2
  • 22
  • 42