sicp 笔记 (9)

第三章习题 3.73 – 3.82 的解答。

E-3.73:

(load "../examples/3.5.2-infinite-streams.scm")

(define (integral integrand initial-value dt)
  (define int
    (cons-stream initial-value
                 (add-streams (scale-stream integrand dt)
                              int)))
  int)

(define (RC resistance capacitance time-step)
  (lambda (currents initial-voltage)
    (cons-stream initial-voltage
                 (add-streams (scale-stream currents resistance)
                              (integral (scale-stream currents (/ 1 capacitance))
                                        initial-voltage
                                        time-step)))))

E-3.74:

(define zero-crossings
  (stream-map sign-change-detector sense-data (stream-cdr sense-data)))

E-3.75: 不理解题目中“spurious zero crossings”怎么判定,找的参考资料 [1]。

(define (make-zero-crossings input-stream last-value last-avpt) 
  (let ((avpt (/ (+ (stream-car input-stream) last-value) 2))) 
    (cons-stream (sign-change-detetor avpt last-avpt) 
                 (make-zero-crossings (stream-cdr input-stream) 
                                      (stream-car input-stream) 
                                      avpt)))) 

E-3.76:

(define (make-zero-crossings input-stream last-value)
  (define (smooth stream)
    (define (recur s l) ; s => input-stream, l => last-value
      (let ((avpt (/ (+ (stream-car s) l) 2)))
        (cons-stream avpt (smooth (stream-cdr s) (stream-car s)))))

    (recur stream last-value))

    (let ((avpt-list (smooth input-stream)))
      (stream-map sign-change-detector avpt-list (stream-cdr avpt-list))))

(define zero-crossings (make-zero-crossings sense-data 0))

E-3.77: 使用 plt-r5rs 运行示例程序会报错,但是使用 mit-scheme 则正常,看出错原因好像是对 stream-map 里的 stream-car 处理有差别,菜鸟表示先不去深究了。

(define (integral delay-integrand initial-value dt)
  (cons-stream
    initial-value
    (let ((integrand (force delay-integrand)))
      (if (stream-null? integrand)
        the-empty-stream
        (integral (stream-cdr integrand)
                  (+ (* dt (stream-car integrand))
                     initial-value)
                  dt)))))

E-3.78: 使用 mit-scheme 测试。

(load "../examples/3.5.4-streams-and-delayed-evaluation.scm")

(define (solve-2nd a b dt y0 dy0)
  (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)

E-3.79: 见参考资料 [2]。

(load "../exercises/3.50.scm")
(load "../examples/3.5.4-streams-and-delayed-evaluation.scm")

(define(general-solve-2nd f y0 dy0 dt)
  (define y (integral (delay dy) y0 dt))
  (define dy (integral (delay ddy) dy0 dt))
  (define ddy (my-stream-map f dy y))
  y)

E-3.80:

(load "../exercises/3.50.scm")
(load "../examples/3.5.4-streams-and-delayed-evaluation.scm")

(define (RLC R L C dt)
  (lambda (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 iL (/ (- R) L))
                             (scale-stream vC (/ 1 L))))

    (my-stream-map cons vC iL)))

; --------------------------------------------------------------

(stream-list ((RLC 1 1 0.2 0.1) 10 0) 10)

E-3.81:

(load "../exercises/3.50.scm")

(define random-init 5)
(define (rand-update num) (+ num 5))

(define (rand-generator cmd-stream)
  (define result-stream
    (cons-stream random-init
                 (my-stream-map (lambda (cmd num)
                                  (cond ((eq? cmd 'generate)
                                         (rand-update num))
                                        ((eq? cmd 'reset)
                                         random-init)
                                        (else
                                          (display "error command"...))))
                                cmd-stream
                                result-stream)))

  result-stream)

; --------------------------------------------------------------------------------

(define cmd-stream
  (cons-stream 'generate cmd-stream))

(stream-list (rand-generator cmd-stream) 10)

E-3.82:

(define (estimate-integral p x1 y1 x2 y2)
  (define internal-p-stream
    (cons-stream (p (random-in-range x1 x2) (random-in-range y1 y2))
                 internal-p-stream))

  (monte-carlo internal-p-stream 0 0))

参考资料

[1] sicp-ex-3.75
[2] sicp-ex-3.79

发表于 2014年3月8日
本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>