이 문제는 직렬 RLC 회로를 구현하는 문제입니다.
앞의 연습문제 3.73에서 RC 회로를 구현하였기에 쉽게 할 수 있었습니다.
하지만 RC 회로와 다른 점은 vC와 iL이 서로를 부른다는 점입니다.

위와 같은 신호 흐름도를 보고 만들어야 합니다.^^
이에 맞춰 프로시저를 만들었습니다.

잘 나오네요.^^
지금은 잘 나오지만 이 문제를 풀 때 한참을 고민하였습니다.
(/ (* -1 R) L)
위 코드를 적을 때 (/ (* -1 R) / L)라고 적어서 에러 메시지가 났습니다.
그런데 그 메시지의 뜻을 이해하지 못해 제가 모듈화에 실패한 듯싶어
이리 고치고 저리 고치고 이리 살피고 저리 살폈습니다.
그러다 결국 /을 잘못 쳤다는 것을 알게 되었고, 그 때 허무함이 가득하였습니다.OTL....
예전에 C를 배울 때 코드를 컴파일 하였더니 에러가 발생했다고 합니다.
하지만 컴파일러가 그리 좋은 것이 아니라
어디에 에러인지 무슨 에러인지 출력하지 않았다고 합니다.
그래서 몇 천줄의 코드를 이틀을 걸쳐 살펴보았더니
결국 ; 하나를 적지 않아 발생한 에러라 좌절했다는 얘기를 들었습니다.
그 때는 '그런 황당한 이유로 에러를 발생시키고 고생하는가?'라고 웃었는데,
지금 제가 겪고나니 웃을 일이 아니더군요.OTL....
참조
해럴드 애빌슨, 김재우 역, <컴퓨터 프로그램의 구조와 해석>, 인사이트, 2007, pp. 456
; stream
(define true (= 0 0))
(define false (= 1 0))
(define (cons-stream a b)
(cons a (delay b)))
(define the-empty-stream '())
(define stream-null? null?)
(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))
; section 3.5
(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))
(define (stream-for-each proc s)
(if (stream-null? s)
'done
(begin (proc (stream-car s))
(stream-for-each proc (stream-cdr s)))))
(define (display-stream s)
(stream-for-each display-line s))
(define (display-line x)
(newline)
(display x))
(define (stream-enumerate-interval low high)
(if (> low high)
the-empty-stream
(cons-stream
low
(stream-enumerate-interval (+ low 1) high))))
(define (stream-filter pred stream)
(cond ((stream-null? stream) the-empty-stream)
((pred (stream-car stream))
(cons-stream (stream-car stream)
(stream-filter pred
(stream-cdr stream))))
(else (stream-filter pred (stream-cdr stream)))))
(define (memo-proc proc)
(let ((already-run? false) (result false))
(lambda ()
(if (not already-run?)
(begin (set! result (proc))
(set! already-run? true)
result)
result))))
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream))
; exercise 3.50
(define (stream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(cons-stream
(apply proc (map stream-car argstreams))
(apply stream-map
(cons proc (map stream-cdr argstreams))))))
;;;SECTION 3.5.2
(define (add-streams s1 s2)
(stream-map + s1 s2))
(define ones (cons-stream 1 ones))
(define integers (cons-stream 1 (add-streams ones integers)))
; exercise 3.54
(define (mul-streams s1 s2)
(stream-map * s1 s2))
; exercise 3.55
(define (partial-sums S)
(cons-stream (stream-car S) (add-streams (stream-cdr S)
(partial-sums S))))
; exercise 3.56
(define (merge s1 s2)
(cond ((stream-null? s1) s2)
((stream-null? s2) s1)
(else
(let ((s1car (stream-car s1))
(s2car (stream-car s2)))
(cond ((< s1car s2car)
(cons-stream s1car (merge (stream-cdr s1) s2)))
((> s1car s2car)
(cons-stream s2car (merge s1 (stream-cdr s2))))
(else
(cons-stream s1car
(merge (stream-cdr s1)
(stream-cdr s2)))))))))
; print-stream-n
(define (print-stream-n S n l)
(define (iter i)
(if (= i n)
'done
(begin (display (stream-ref S i))
(display " ")
(if (= (remainder (+ i 1) l) 0)
(newline))
(iter (+ i 1)))))
(iter 0))
; exercise 3.59
(define (div-streams s1 s2)
(stream-map / s1 s2))
; exercise 3.77
(define (integral delayed-integrand initial-value dt)
(define (int integrand initial-value)
(cons-stream initial-value
(if (stream-null? integrand)
the-empty-stream
(int (stream-cdr integrand)
(+ (* dt (stream-car integrand))
initial-value)))))
(int (force delayed-integrand) initial-value))
; exercise 3.80
(define (RLC R L C dt)
(define (circuit 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 (/ (* -1 R) L))))
(stream-map (lambda (x y) (cons x y)) vC iL))
circuit)
; execute
(define RLC-circuits ((RLC 1 1 0.2 0.1) 10 0))
(print-stream-n RLC-circuits 10 1)
- SICP Exercise 연습문제 3장 소스 파일 (0)2008/07/30
- SICP Exercise 연습문제 3.82 (0)2008/07/30
- SICP Exercise 연습문제 3.81 (0)2008/07/29
- SICP Exercise 연습문제 3.80 (0)2008/07/29
- SICP Exercise 연습문제 3.79 (0)2008/07/29
- SICP Exercise 연습문제 3.78 (0)2008/07/29
- SICP Exercise 연습문제 3.77 (0)2008/07/29
글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.







댓글을 달아 주세요