(define (enumerate-to from to step)
(if (> from to)
the-empty-stream
(cons-stream from (enumerate-to (+ from step) to step))))
(define (map s action)
(if (eq? the-empty-stream s)
the-empty-stream
(cons-stream (action (head s)) (map (tail s) action))))
(define (combine s1 s2 combiner)
(cond ((eq? (head s1) the-empty-stream) s2)
((eq? (head s2) the-empty-stream) s1)
(else
(cons-stream
(combiner (head s1) (head s2))
(combine (tail s1) (tail s2) combiner)))))
(define (accumulate-n operator S)
(cond ((empty-stream? s) the-empty-stream)
((empty-stream? (tail s)) (head s))
(else
(accumulate-n operator
(cons-stream
(combine (head s) (head (tail s)) operator)
(tail (tail s)))))))
(define triplet-stream
(map
(enumerate-to 0 3 1)
(lambda (x) (enumerate-to (1+ (* 3 x)) (+ 3 (* 3 x)) 1))))
(define accu-stream (accumulate-n + triplet-stream))
(define (first n stream)
(if (= n 0)
(begin
(newline)
the-non-printing-object)
(begin
(display (head stream))
(display " ")
(first (-1+ n) (tail stream)))))