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

Exercise 3.68 · 习题

Exercise 3.68: Louis Reasoner thinks that
building a stream of pairs from three parts is unnecessarily complicated.
Instead of separating the pair (

S
0

,

T
0

) from the rest of the pairs in
the first row, he proposes to work with the whole first row, as follows:

(define (pairs s t)
(interleave
(stream-map
(lambda (x)
(list (stream-car s) x))
t)
(pairs (stream-cdr s)
(stream-cdr t))))

Does this work? Consider what happens if we evaluate (pairs integers

integers) using Louis’s definition of pairs.

练习 3.68:Louis Reasoner 认为,从三个部分构建序对的流不必要地复杂。他提议不将 (S₀, T₀) 与第一行中其余序对分离,而是直接处理整个第一行,如下所示:

(define (pairs s t)
(interleave
(stream-map
(lambda (x)
(list (stream-car s) x))
t)
(pairs (stream-cdr s)
(stream-cdr t))))

这行得通吗?考虑用 Louis 的 pairs 定义对 (pairs integers integers) 求值时会发生什么。

Racket #lang sicp
(define (pairs s t)
 (interleave
 (stream-map
 (lambda (x)
 (list (stream-car s) x))
 t)
 (pairs (stream-cdr s)
 (stream-cdr t))))