灯下 登录
计算机科学 / SICP / 3.5.3 Exploiting the Stream Paradigm

Exercise 3.74 · 习题

Exercise 3.74: Alyssa P. Hacker is designing a
system to process signals coming from physical sensors. One important feature
she wishes to produce is a signal that describes the
zero crossings
of the input signal. That is, the resulting signal should be +
1 whenever the
input signal changes from negative to positive, −
1 whenever the input signal
changes from positive to negative, and 0 otherwise. (Assume that the sign of a
0 input is positive.) For example, a typical input signal with its associated
zero-crossing signal would be

… 1 2 1.5 1 0.5 -0.1 -2 -3 -2 -0.5 0.2 3 4 …
… 0 0 0 0 0 -1 0 0 0 0 1 0 0 …

In Alyssa’s system, the signal from the sensor is represented as a stream
sense-data and the stream zero-crossings is the corresponding
stream of zero crossings. Alyssa first writes a procedure
sign-change-detector that takes two values as arguments and compares the
signs of the values to produce an appropriate 0, 1, or −
1. She then
constructs her zero-crossing stream as follows:

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

Alyssa’s boss, Eva Lu Ator, walks by and suggests that this program is
approximately equivalent to the following one, which uses the generalized
version of stream-map from Exercise 3.50:

(define zero-crossings
(stream-map sign-change-detector
sense-data
⟨expression⟩))

Complete the program by supplying the indicated ⟨expression⟩.

练习 3.74:Alyssa P. Hacker 正在设计一个处理物理传感器信号的系统。她希望实现的一个重要特性是生成描述输入信号零交叉点 (zero crossings) 的信号:当输入信号从负变正时,结果信号应为 +1;当输入信号从正变负时,结果信号应为 −1;其他情况下为 0。(假设 0 输入的符号为正。)例如,一段典型的输入信号及其对应的零交叉信号如下:

… 1 2 1.5 1 0.5 -0.1 -2 -3 -2 -0.5 0.2 3 4 …
… 0 0 0 0 0 -1 0 0 0 0 1 0 0 …

在 Alyssa 的系统中,传感器信号表示为流 sense-data,而流 zero-crossings 是对应的零交叉流。Alyssa 首先写了一个过程 sign-change-detector,它接受两个值作为参数,通过比较两值的符号来产生适当的 0、1 或 −1。然后她按如下方式构造零交叉流:

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

Alyssa 的上司 Eva Lu Ator 路过时建议说,这个程序大致等价于下面这个使用练习 3.50 中广义 stream-map 的版本:

(define zero-crossings
(stream-map sign-change-detector
sense-data
⟨expression⟩))

请通过补全 ⟨expression⟩ 来完成这个程序。

Racket #lang sicp
… 1 2 1.5 1 0.5 -0.1 -2 -3 -2 -0.5 0.2 3 4 …
… 0 0 0 0 0 -1 0 0 0 0 1 0 0 …
Racket #lang sicp
(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))
Racket #lang sicp
(define zero-crossings
 (stream-map sign-change-detector
 sense-data
 ⟨expression⟩))