From: Ramakrishnan Muthukrishnan Date: Sat, 6 Aug 2011 19:20:02 +0000 (+0530) Subject: Solutions to 3.73, 3.74, 3.75, 3.76, 3.77, 3.78, 3.79, 3.80 and 3.81 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/%22doc.html/frontends/about.html?a=commitdiff_plain;h=750dfba1d800d9a0404497a87182a2a89942d328;p=sicp.git Solutions to 3.73, 3.74, 3.75, 3.76, 3.77, 3.78, 3.79, 3.80 and 3.81 --- diff --git a/src/sicp/ex3_73.rkt b/src/sicp/ex3_73.rkt new file mode 100644 index 0000000..ff0abd3 --- /dev/null +++ b/src/sicp/ex3_73.rkt @@ -0,0 +1,9 @@ +#lang racket + +(define (RC R C dt) + (define (RC1 i initial-vc) + (add-streams (integral (scale-stream i (/ 1.0 C)) initial-vc dt) + (scale-stream i R))) + RC1) + +(define RC1 (RC 5 1 0.5)) \ No newline at end of file diff --git a/src/sicp/ex3_74.rkt b/src/sicp/ex3_74.rkt new file mode 100644 index 0000000..9d5bb78 --- /dev/null +++ b/src/sicp/ex3_74.rkt @@ -0,0 +1,21 @@ +#lang racket + +(define (make-zero-crossings input-stream last-value) + (cons-stream + (sign-change-detector (stream-car input-stream) last-value) + (make-zero-crossings (stream-cdr input-stream) + (stream-car input-stream)))) + +(define (sign-change-detector current-value previous-value) + (cond [(and (positive? previous-value) + (positive? current-value)) 0] + [(and (positive? previous-value) + (negative? current-value)) -1] + [(and (negative? previous-value?) + (positive? current-value)) +1] + [else 0])) + +(define zero-crossings (make-zero-crossings sense-data 0)) + +;; eva's implementation +(define zc (stream-map sign-change-detector sense-data (cons-stream 0 sense-data))) diff --git a/src/sicp/ex3_75.rkt b/src/sicp/ex3_75.rkt new file mode 100644 index 0000000..f569b32 --- /dev/null +++ b/src/sicp/ex3_75.rkt @@ -0,0 +1,12 @@ +#lang racket + +#| + +The procedure find average of the current sensor value with the last average value. This +is wrong. the symbol 'last-value' is used differently in different places. In the average +calculation, this should be the last sensor value. In the sign-change-detector this should +be last average value. + +Solution is for the make-zero-crossing to accept the last-average-value as another parameter. + +|# \ No newline at end of file diff --git a/src/sicp/ex3_76.rkt b/src/sicp/ex3_76.rkt new file mode 100644 index 0000000..f4f6fe1 --- /dev/null +++ b/src/sicp/ex3_76.rkt @@ -0,0 +1,17 @@ +#lang racket + +(define (smooth stream) + (cons-stream (/ (+ (stream-car stream) + (stream-car (stream-cdr stream))) + 2.0) + (smooth (stream-cdr stream)))) + +(define (make-zero-crossings input-stream last-value) + (cons-stream + (sign-change-detector (stream-car input-stream) last-value) + (make-zero-crossings (stream-cdr input-stream) + (stream-car input-stream)))) + +(define zero-crossings (make-zero-crossings (smooth sense-data) 0)) + + \ No newline at end of file diff --git a/src/sicp/ex3_77.rkt b/src/sicp/ex3_77.rkt new file mode 100644 index 0000000..c7de078 --- /dev/null +++ b/src/sicp/ex3_77.rkt @@ -0,0 +1,11 @@ +#lang racket + +(define (integral integrand delayed-initial-value dt) + (cons-stream initial-value + (let ((integrand (force delayed-initial-value))) + (if (stream-null? integrand) + the-empty-stream + (integral (stream-cdr integrand) + (+ (* dt (stream-car integrand)) + initial-value) + dt))))) \ No newline at end of file diff --git a/src/sicp/ex3_78.rkt b/src/sicp/ex3_78.rkt new file mode 100644 index 0000000..3a1282a --- /dev/null +++ b/src/sicp/ex3_78.rkt @@ -0,0 +1,9 @@ +#lang racket + +(define (solve-2nd a b y0 dy0 dt) + (define y (integral (delay dy) y0 dt)) + (define dy (integral (delay ddy) dy0 dt)) + (define ddy (add-streams (scale-stream dy a) + (scale-stream y b))) + y) + diff --git a/src/sicp/ex3_79.rkt b/src/sicp/ex3_79.rkt new file mode 100644 index 0000000..4fa6093 --- /dev/null +++ b/src/sicp/ex3_79.rkt @@ -0,0 +1,7 @@ +#lang racket + +(define (solve-2nd f y0 dy0 dt) + (define y (integral (delay dy) y0 dt)) + (define dy (integral (delay ddy) dy0 dt)) + (define ddy (stream-map f dy y)) + y) \ No newline at end of file diff --git a/src/sicp/ex3_80.rkt b/src/sicp/ex3_80.rkt new file mode 100644 index 0000000..989c6d7 --- /dev/null +++ b/src/sicp/ex3_80.rkt @@ -0,0 +1,12 @@ +#lang racket + +(define (RLC R L C dt) + (define (rlc1 vc0 il0) + (define vc (integral (delay dvc) vc0 dt)) + (define dvc0 (scale-stream il (/ -1 C))) + (define il (integral (delay dil) il0 dt)) + (define dil (add-streams (scale-stream vc (/ 1 L)) + (scale-stream il (/ -R L)))) + (stream-map (lambda (x y) (cons x y)) vc il))) + + diff --git a/src/sicp/ex3_81.rkt b/src/sicp/ex3_81.rkt new file mode 100644 index 0000000..158bec4 --- /dev/null +++ b/src/sicp/ex3_81.rkt @@ -0,0 +1,17 @@ +#lang racket + +(define (rand-seq init req-stream) + (let ((req (stream-car req-stream))) + (cond ((eq? req 'reset) + (let ((new-init (stream-car (stream-cdr req-stream)))) + (let ((new-rand-val (rand-update new-init))) + (cons-stream + new-rand-val + (rand-seq new-rand-val + (stream-cdr (stream-cdr req-stream))))))) + ((eq? req 'generate) + (let ((new-val (rand-update init))) + (cons-stream + new-val + (rand-seq new-val (stream-cdr req-stream)))))))) + \ No newline at end of file