diff --git a/ch3/ex3-59.scm b/ch3/ex3-59.scm new file mode 100644 index 0000000..6457c06 --- /dev/null +++ b/ch3/ex3-59.scm @@ -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))) \ No newline at end of file diff --git a/ch3/ex3-60.scm b/ch3/ex3-60.scm new file mode 100644 index 0000000..58e5454 --- /dev/null +++ b/ch3/ex3-60.scm @@ -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) \ No newline at end of file diff --git a/ch3/ex3-61.scm b/ch3/ex3-61.scm new file mode 100644 index 0000000..6e52146 --- /dev/null +++ b/ch3/ex3-61.scm @@ -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) \ No newline at end of file diff --git a/ch3/ex3-62.scm b/ch3/ex3-62.scm new file mode 100644 index 0000000..983f0b9 --- /dev/null +++ b/ch3/ex3-62.scm @@ -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 \ No newline at end of file