sicp-ex-3.50



<< Previous exercise (3.49) | Index | Next exercise (3.51) >>


meteorgan



(define (stream-map proc . argstreams)
  (if (stream-null? (car argstreams))
      the-empty-stream
      (cons-stream
        (apply proc (map stream-car argstreams))
        (apply stream-map 
               (cons proc (map stream-cdr argstreams))))))

That implementation assumes that the streams are the same length (perhaps infinite). In order to cope with streams of different lengths I have this:

 (define (filter predicate list) 
   (cond ((null? list) 
          '()) 
         ((predicate (car list)) 
          (cons (car list) (filter predicate (cdr list)))) 
         (else 
          (filter predicate (cdr list))))) 
 (define (stream-map proc . argstreams) 
   (let ((non-null-args (filter (lambda (s) (not (stream-null? s))) argstreams))) 
     (if (null? non-null-args) 
       the-empty-stream 
       (cons-stream 
        (apply proc (map stream-car non-null-args)) 
        (apply stream-map 
               (cons proc (map stream-cdr non-null-args)))))))