読者です 読者をやめる 読者になる 読者になる

(wat-aro)

無職から有職者にランクアップしました

SICP 問題 3.74

scheme SICP

last-valueを取る代わりにmapのargstreamsの二つ目に一回分遅らせたsense-dataをとればいい.
そうすれば今の値と前回の値を比べて零交差だったかをsign-change-detectorで判定できる.

(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 sense-data 0))

(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))))))

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