sicp-ex-3.60



<< Previous exercise (3.59) | Index | Next exercise (3.61) >>


meteorgan



(define (mul-series s1 s2)
  (cons-stream (* (stream-car s1) (stream-car s2))
               (add-streams (scale-stream (stream-cdr s2) (stream-car s1)) 
                                        (mul-series (stream-cdr s1) s2))))

(define (mul-series s1 s2)
  (cons-stream (* (stream-car s1)
                  (stream-car s2))
               (add-streams (mul-streams (stream-cdr s1)
                                         (stream-cdr s2))
                            (mul-series s1 s2))))

(define (mul-series2 s1 s2)
  (partial-sums (mul-streams s1 s2)))



(define (mul-series s1 s2)
  (cons-stream
    (* (stream-car s1) (stream-car s2))
    (add-streams (add-streams (scale-stream (stream-cdr s1) (stream-car s2))
                              (scale-stream (stream-cdr s2) (stream-car s1)))
                 (cons-stream 0 (mul-series (stream-cdr s1) (stream-cdr s2))))))



atupal is correct




seok

meteorgan and atupal are correct and zzd3zzd is wrong according to my checker.

  
  
 (define (display-stream-until n s)     ; n-th value included 
     (if (< n 0) 
         the-empty-stream 
         (begin (newline) (display (stream-car s)) 
                (display-stream-until (- n 1) (stream-cdr s))))) 
  
 ; It can be easily seen that (mul-series (stream-cdr s1) s2) equals 
 ; (add-streams (scale-stream (stream-cdr s2) (stream-car s1)) 
 ;              (cons-stream 0 (mul-series (stream-cdr s1) (stream-cdr s2)))) 
 ; , which means that meteorgan's solution and atupal's solution are equivalent. 
 (define (mul-series s1 s2) 
     (cons-stream (* (stream-car s1) (stream-car s2)) 
                  (add-streams (scale-stream (stream-cdr s1) (stream-car s2)) 
                               (mul-series (stream-cdr s2) s1)))) 
  
 (define circle-series 
     (add-streams (mul-series cosine-series cosine-series) 
                  (mul-series sine-series sine-series))) 
  
 ; if you see one 1 followed by 0's, your mul-series is correct. 
 (display-stream-until 30 circle-series) 
  
  

Patrick-OHara

I agree with seok. However, in testing with non-infinite series I found that the book's version of stream-map does not cope with series of different lengths, and so gives an incorrect result for mul-series applied to non-infinite series. See http://community.schemewiki.org/?sicp-ex-3.50 for my fix for this.


joshroybal




(define (mul-series s1 s2)
  (cons-stream (* (stream-car s1) (stream-car s2))
               (add-streams
                (scale-stream (stream-cdr s2) (stream-car s1))
                (mul-series (stream-cdr s1) s2))))