sicp-ex-3.62



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


meteorgan



(define (div-series s1 s2)
  (let ((c (stream-car s2)))
    (if (= c 0)
        (error "constant term of s2 can't be 0!")
        (scale-stream (mul-series s1
                                  (reciprocal-series (scale-stream s2 (/ 1 c))))
                      (/ 1 c)))))

(define tane-series (div-series sine-series cosine-series))

I'm pretty sure the tan series won't work like that because half the coefficients for cos are zero.



gravitykey

; according 3-61
; S·X = 1
; X = 1 / S
; another S1
; S1 / S  =  S1 · X

;use code in 3-61
(load "3-61.scm")
;"daoshu" mean reciprocal

(define (div-series s1 s2)
    (cond ((eq? 0 (stream-car s2)) (error "constant term of s2 can't be 0!"))
          (else (mul-series s1 (daoshu s2)))))

tests

(define sine-series (cons-stream 0 (integrate-series cosine-series)))
(define cosine-series (cons-stream 1 (integrate-series (stream-map (lambda (x) (* x -1)) sine-series))))
(define (add-streams s1 s2)(stream-map + s1 s2))

(define (integrate-series stm)
    (define (inner s a)
        (cons-stream (* (/ 1 a) (stream-car s)) 
                     (inner (stream-cdr s) (+ a 1))))
    (inner stm 1))

(define tan-series (div-series sine-series cosine-series))
(stream-head tan-series 10)

;Value 13: (0 1 0 1/3 0 2/15 0 17/315 0 62/2835)
;It's correct.