# weima learns to program

my attempt to do the exercises in sicp.

## Sunday, December 19, 2010

### sicp exercise 3.59

;; Exercise 3.59.  In section 2.5.3 we saw how to implement a polynomial arithmetic system representing polynomials as lists of terms. In a similar way, we can work with power series, such as
;;

;;
;; represented as infinite streams. We will represent the series a0 + a1 x + a2 x2 + a3 x3 + Â·Â·Â· as the stream whose elements are the coefficients a0, a1, a2, a3, ....
;;
;; a. The integral of the series a0 + a1 x + a2 x2 + a3 x3 + Â·Â·Â· is the series
;;

;;
;; where c is any constant. Define a procedure integrate-series that takes as input a stream a0, a1, a2, ... representing a power series and returns the stream a0, (1/2)a1, (1/3)a2, ... of coefficients of the non-constant terms of the integral of the series. (Since the result has no constant term, it doesn't represent a power series; when we use integrate-series, we will cons on the appropriate constant.)
;;
;; b. The function x->e^x is its own derivative. This implies that ex and the integral of ex are the same series, except for the constant term, which is e0 = 1. Accordingly, we can generate the series for ex as
;;
;; (define exp-series
;;   (cons-stream 1 (integrate-series exp-series)))
;;
;; Show how to generate the series for sine and cosine, starting from the facts that the derivative of sine is cosine and the derivative of cosine is the negative of sine:
;;
;; (define cosine-series
;;   (cons-stream 1 <??>))
;; (define sine-series
;;   (cons-stream 0 <??>))

(define (integrate-series s)
(define (integrate-series-i s n)
(cons-stream  (/ (stream-car s) n)
(integrate-series-i (stream-cdr s) (+ 1 n))))
(integrate-series-i s 1))

(define (mul-stream s1 s2)
(stream-map * s1 s2))

(define minus-one
(cons-stream -1 minus-one))

(define cosine-series
(cons-stream 1 (mul-stream minus-one (integrate-series sine-series))))

(define sine-series
(cons-stream 0 (integrate-series cosine-series)))

(newline)
(display (stream-ref sine-series 0))(newline)
(display (stream-ref sine-series 1))(newline)
(display (stream-ref sine-series 2))(newline)
(display (stream-ref sine-series 3))(newline)
(display (stream-ref sine-series 4))(newline)
(display (stream-ref sine-series 5))(newline)
(display (stream-ref sine-series 6))(newline)
(display (stream-ref sine-series 7))(newline)
(display (stream-ref sine-series 8))(newline)
(display "----------------------")(newline)
(display (stream-ref cosine-series 0))(newline)
(display (stream-ref cosine-series 1))(newline)
(display (stream-ref cosine-series 2))(newline)
(display (stream-ref cosine-series 3))(newline)
(display (stream-ref cosine-series 4))(newline)
(display (stream-ref cosine-series 5))(newline)
(display (stream-ref cosine-series 6))(newline)
(display (stream-ref cosine-series 7))(newline)
(display (stream-ref cosine-series 8))(newline)

;; Output:
;0
;1
;0
;-1/6
;0
;1/120
;0
;-1/5040
;0
;----------------------
;1
;0
;-1/2
;0
;1/24
;0
;-1/720
;0
;1/40320
;... done