Exercise 3.50. Complete the following definition, which generalizes stream-map to allow procedures that take multiple arguments, analogous to map in section 2.2.3, footnote 12.
(define (stream-map proc . argstreams)
(if (<??> (car argstreams))
the-empty-stream
(<??>
(apply proc (map <??> argstreams))
(apply stream-map
(cons proc (map <??> argstreams))))))
我们有下面这样写一个 map 的经验:
(define (my-map proc . args)
(define (map-list proc items)
(if (null? items)
'()
(cons (proc (car items))
(map-list proc (cdr items)))))
(if (null? (car args))
'()
(cons
(apply proc (map-list car args))
(apply my-map (cons proc (map-list cdr args))))))
;;; tests begin
; tests will be taken from section 3.5.2
(define (add-streams s1 s2)
(stream-map + s1 s2))
同理,我们可以写出有流结构的 map
(load "stream.scm")
(define (stream-map proc . argstreams)
(if (null? (stream-car argstreams))
the-empty-stream
(cons-stream
(apply proc (map stream-car argstreams))
(apply stream-map
(cons proc (map stream-cdr argstreams))))))