乐正

Actions speak louder than words.

Sicp-ex3-50

问题

请完成下面的定义,这个过程是stream-map的推广,它允许过程带有多个参数,类似于2.2.3节的脚注78.

1
2
3
4
5
6
7
(define (stream-map proc . argstreams)
  (if (<??> (car argstreams))
      the-empty-stream
      (<??>
        (apply proc (map <??> argstreams))
        (apply stream-map
               (cons proc (map <??> argstreams))))))

解答

练习3.50 (ex3-50.scm) download
1
2
3
4
5
6
7
(define (stream-map proc . argstreams)
  (if (stream-null? (car argstreams))
      (stream)
      (cons-stream
       (apply proc (map stream-car argstreams))
       (apply stream-map
              (cons proc (map stream-cdr argstreams))))))

测试

1
2
3
4
5
6
(define result (stream-map +
                           (list->stream '(3 4 5 6))
                           (list->stream '(4 5 6 7))))

(stream->list result)
;Value 41: (7 9 11 13)

draft

« sicp-ex3-44 sicp-ex3-51 »

Comments