ruandao / blog

0 stars 0 forks source link

[12.23] [sicp 3.60] #110

Closed ruandao closed 8 years ago

ruandao commented 8 years ago

(load "3.59.scm")

(define (mul-series s1 s2)
  (cons-stream (* (stream-car s1)
          (stream-car s2))
           (add-stream (stream-map (lambda(x) (* (stream-car s1)
                             x))
                       (stream-cdr s2))
               (stream-map (lambda(x) (* (stream-car s2)
                             x))
                       (stream-cdr s1)))))
#|

 ((lambda()
   (define s2 (mul-series cosine-series cosine-series))
   (define c2 (mul-series sine-series sine-series))
   (define r (add-stream s2 c2))
   (define (sum items s)
     (cond ((null? items) s)
           (else (sum (cdr items) (+ s (car items))))))
   (sum (cdr (stream-head 'r r 10)) 0)

   ;(sum (cdr (stream-head 'ones (mul-series ones ones) 20)) 0)
 ))
|#
ruandao commented 8 years ago

参考了别人的, 改正下


(load "3.59.scm")

(define (mul-series-1 s1 s2)
  (cons-stream (* (stream-car s1)
          (stream-car s2))
           (add-stream (stream-map (lambda(x) (* (stream-car s1)
                             x))
                       (stream-cdr s2))
               (stream-map (lambda(x) (* (stream-car s2)
                             x))
                       (stream-cdr s1)))))

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

  ((lambda()
   (define s2 (mul-series cosine-series cosine-series))
   (define c2 (mul-series sine-series sine-series))
   (define r (add-stream s2 c2))
   (define (sum items s)
     (cond ((null? items) s)
           (else (sum (cdr items) (+ s (car items))))))
   (sum (cdr (stream-head 'r r 100)) 0.0)

   ;(sum (cdr (stream-head 'ones (mul-series ones ones) 20)) 0) ; ;
   ))

|#
ruandao commented 8 years ago

根据别人的改了下...


(load "3.59.scm")

(define (mul-series-1 s1 s2)
  (cons-stream (* (stream-car s1)
          (stream-car s2))
           (add-stream (stream-map (lambda(x) (* (stream-car s1)
                             x))
                       (stream-cdr s2))
               (stream-map (lambda(x) (* (stream-car s2)
                             x))
                       (stream-cdr s1)))))

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

  ((lambda()
   (define s2 (mul-series cosine-series cosine-series))
   (define c2 (mul-series sine-series sine-series))
   (define r (add-stream s2 c2))
   (define (sum items s)
     (cond ((null? items) s)
           (else (sum (cdr items) (+ s (car items))))))
   (sum (cdr (stream-head 'r r 10)) 0.0)

   ;(sum (cdr (stream-head 'ones (mul-series ones ones) 20)) 0) ; ;
   ))

|#