Mul-streams procedure

Define a procedure mul-streams , analogous to add-streams , that produces the elementwise product of its two input streams. Use this together with the stream of integers to complete the following definition of the stream whose n th element (counting from 0 ) is n + 1 factorial:

(define factorials (cons-stream 1 (mul-streams <??> <??>)))

There are no comments yet.

Authentication required

You must log in to post a comment.

(define (stream-car stream) (car stream))

(define (stream-cdr stream) (force (cdr stream)))

(define (stream-ref s n)
  (if (= n 0)
      (stream-car s)
      (stream-ref (stream-cdr s) (- n 1))))

(define ones (cons-stream 1 ones))

(define (stream-map proc . list-of-stream)
    (if (null? (car list-of-stream))
            (apply proc 
                   (map (lambda (s)
                            (stream-car s))
            (apply stream-map 
                   (cons proc (map (lambda (s)
                                       (stream-cdr s))

(define (add-streams s1 s2)
  (stream-map + s1 s2))

(define integers (cons-stream 1 (add-streams ones integers)))

(check-equal? (stream-ref (mul-streams integers integers) 0) 1)
(check-equal? (stream-ref (mul-streams integers integers) 1) 4)

(check-equal? (stream-ref factorials 0) 1)
(check-equal? (stream-ref factorials 2) 6)
(check-equal? (stream-ref factorials 3) 24)