<< Previous exercise (3.79) | Index | Next exercise (3.81) >>


 (define (RLC R L C dt) 
         (define (rcl vc0 il0) 
                 (define vc (integral (delay dvc) vc0  dt)) 
                 (define il (integral (delay dil) il0 dt)) 
                 (define dvc (scale-stream il (- (/ 1 C)))) 
                 (define dil (add-streams (scale-stream vc (/ 1 L)) 
                                                                 (scale-stream il (- (/ R L))))) 
                 (define (merge-stream s1 s2) 
                         (cons-stream (cons (stream-car s1) (stream-car s2)) 
                                                  (merge-stream (stream-cdr s1) (stream-cdr s2)))) 
                 (merge-stream vc il)) 

why not simply (cons vc il) at the end of the procedure instead of merging? the exercise says "a pair of the streams".

meteorgan has redefined the merging method. You idiot!

it's not necessary to insult people


 We could first compute the integral then scale the stream 
 (define (RLC R L C dt)  
   (define (proc vc0 il0) 
     (define vc (scale-stream (integral (delay il) (* (- C) vc0) dt) (/ -1 C))) 
     (define il (integral (delay dil) il0 dt)) 
     (define dil (add-streams (scale-stream il (/ (- R) L))  
                              (scale-stream vc (/ 1 L)))) 
     (stream-map cons vc il)) 


Two quick points:

1) I interpreted the "pair of streams" as a simple consing instead of a mapping/merging (which produced a "stream of pairs".

2) More importantly, the hack mentioned in 4.1.6 seems necessary to run this in the #lang sicp environment in Racket so I have written out my solution in case you find too that your solution doesnt work in Racket...

Now the solution doesnt really need the definitions of dvC and diL so four lines could be removed from below code but I think the longer code is clearer...

 (define (RLC R L C dt) 
   (lambda (vC-init iL-init) 
     (let ((vC '*unassigned*) 
           (iL '*unassigned*) 
           (dvC '*unassigned*) 
           (diL '*unassigned*)) 
       (set! vC (d-integral (delay dvC) vC-init dt)) 
       (set! iL (d-integral (delay diL) iL-init dt)) 
       (set! dvC (stream-map (lambda(x) (/ (- x) C)) iL)) 
       (set! diL (stream-map (lambda (x y) (+ (/ x L)(/ (* (- R) y) L))) vC iL)) 
       (cons vC iL)))) 


Here is my solution. The exercise asks for pair of streams instead of stream of pairs. Therefore, I just cons-ed the two streams instead of doing mapping on two streams.

 (define (RLC R L C dt) 
   (lambda (vC0 iL0) 
     (define vC (integral (delay dvC) vC0 dt)) 
     (define iL (integral (delay diL) iL0 dt)) 
     (define diL (add-streams (scale-stream vC (/ 1 L)) 
                              (scale-stream iL (/ (- R) L)))) 
     (define dvC (scale-stream iL (/ -1 C))) 
     (cons vC iL)))