Permalink
Browse files

ch3

  • Loading branch information...
1 parent f86fd87 commit 0f2740d8b6fd7aa1b06f728dab7dad54eeaaf3d2 @flaming0 committed Aug 23, 2012
Showing with 164 additions and 0 deletions.
  1. +31 −0 ch3/ex3-59.scm
  2. +51 −0 ch3/ex3-60.scm
  3. +65 −0 ch3/ex3-61.scm
  4. +17 −0 ch3/ex3-62.scm
View
@@ -0,0 +1,31 @@
+;; Example 3.59
+
+#lang racket
+
+(require racket/stream)
+
+;; accumulate stream n times
+
+(define (fold-stream stream n)
+ (if (<= n 0)
+ 0
+ (+ (stream-first stream)
+ (fold-stream (stream-rest stream) (- n 1)))))
+
+(define (integrate-series stream)
+ (define (iter str counter)
+ (stream-cons (* (/ 1. counter) (stream-first str))
+ (iter (stream-rest str) (+ counter 1))))
+ (iter stream 1))
+
+(define exp-series
+ (stream-cons 1 (integrate-series exp-series)))
+
+(fold-stream exp-series 100)
+
+(define cosine-series
+ (stream-cons 1 (stream-map (lambda (x) (- x))
+ (integrate-series sine-series))))
+
+(define sine-series
+ (stream-cons 0 (integrate-series cosine-series)))
View
@@ -0,0 +1,51 @@
+;; Example 3.60
+
+#lang racket
+
+(require racket/stream)
+
+;; scale a stream
+
+(define (scale-stream stream factor)
+ (stream-map (lambda (x) (* x factor)) stream))
+
+;; require for add streams
+
+(define (my-stream-map proc . argstreams)
+ (if (stream-empty? (car argstreams))
+ empty-stream
+ (stream-cons
+ (apply proc (map stream-first argstreams))
+ (apply my-stream-map
+ (cons proc (map stream-rest argstreams))))))
+
+(define (add-streams s1 s2)
+ (my-stream-map + s1 s2))
+
+;; from 3.59
+
+(define (integrate-series stream)
+ (define (iter str counter)
+ (stream-cons (* (/ 1. counter) (stream-first str))
+ (iter (stream-rest str) (+ counter 1))))
+ (iter stream 1))
+
+(define cosine-series
+ (stream-cons 1 (stream-map (lambda (x) (- x))
+ (integrate-series sine-series))))
+
+(define sine-series
+ (stream-cons 0 (integrate-series cosine-series)))
+
+;; 3.60
+
+(define (mul-series s1 s2)
+ (stream-cons (* (stream-first s1) (stream-first s2))
+ (add-streams (scale-stream (stream-rest s2) (stream-first s1))
+ (mul-series (stream-rest s1) s2))))
+
+(define S (add-streams (mul-series sine-series sine-series) (mul-series cosine-series cosine-series)))
+
+(stream-ref S 0)
+(stream-ref S 1)
+(stream-ref S 2)
View
@@ -0,0 +1,65 @@
+;; Example 3.61
+
+#lang racket
+
+;; scale a stream
+
+(define (scale-stream stream factor)
+ (stream-map (lambda (x) (* x factor)) stream))
+
+;; require for add streams
+
+(define (my-stream-map proc . argstreams)
+ (if (stream-empty? (car argstreams))
+ empty-stream
+ (stream-cons
+ (apply proc (map stream-first argstreams))
+ (apply my-stream-map
+ (cons proc (map stream-rest argstreams))))))
+
+(define (add-streams s1 s2)
+ (my-stream-map + s1 s2))
+
+;; from 3.59
+
+(define (integrate-series stream)
+ (define (iter str counter)
+ (stream-cons (* (/ 1. counter) (stream-first str))
+ (iter (stream-rest str) (+ counter 1))))
+ (iter stream 1))
+
+(define cosine-series
+ (stream-cons 1 (stream-map (lambda (x) (- x))
+ (integrate-series sine-series))))
+
+(define sine-series
+ (stream-cons 0 (integrate-series cosine-series)))
+
+;; from 3.60
+
+(define (mul-series s1 s2)
+ (stream-cons (* (stream-first s1) (stream-first s2))
+ (add-streams (scale-stream (stream-rest s2) (stream-first s1))
+ (mul-series (stream-rest s1) s2))))
+
+;; 3.61
+
+(define (invert-unit-series S)
+ (define res (stream-cons 1 (stream-map (λ (x) (- x))
+ (mul-series (stream-rest S) res))))
+ res)
+
+;; test
+
+(define (fold-stream stream n)
+ (if (<= n 0)
+ 0
+ (+ (stream-first stream)
+ (fold-stream (stream-rest stream) (- n 1)))))
+
+(define exp-series
+ (stream-cons 1 (integrate-series exp-series)))
+
+(define one-div-e (invert-unit-series exp-series))
+
+(fold-stream one-div-e 10)
View
@@ -0,0 +1,17 @@
+;; Example 3.62
+
+#lang racket
+
+(require racket/stream)
+
+;; without testing
+
+(define (div-series S1 S2)
+ (let ((i2 (stream-first S2)))
+ (if (= 0 i2)
+ (error "Division by zero")
+ (scale-stream (mul-series S1
+ (invert-unit-series (scale-stream S2 (/ 1. i2))))
+ (/ 1. i2)))))
+
+;; tg = sin/cos

0 comments on commit 0f2740d

Please sign in to comment.