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

Exercise 3.63 · 习题

Exercise 3.63: Louis Reasoner asks why the
sqrt-stream procedure was not written in the following more
straightforward way, without the local variable guesses:

(define (sqrt-stream x)
(cons-stream
1.0
(stream-map (lambda (guess)
(sqrt-improve guess x))
(sqrt-stream x))))

Alyssa P. Hacker replies that this version of the procedure is considerably

less efficient because it performs redundant computation. Explain Alyssa’s

answer. Would the two versions still differ in efficiency if our

implementation of delay used only (lambda () ⟨exp⟩) without

using the optimization provided by memo-proc (3.5.1)?

练习 3.63:Louis Reasoner 问道,为什么 sqrt-stream 过程不按以下更直接的方式编写——不使用局部变量 guesses:

(define (sqrt-stream x)
(cons-stream
1.0
(stream-map (lambda (guess)
(sqrt-improve guess x))
(sqrt-stream x))))

Alyssa P. Hacker 回答说,这个版本的过程效率明显较低,因为它执行了冗余计算。请解释 Alyssa 的回答。如果我们对 delay 的实现只用 (lambda () ⟨exp⟩) 而不使用 memo-proc(3.5.1 节)提供的优化,这两个版本的效率还会有所不同吗?

Racket #lang sicp
(define (sqrt-stream x)
 (cons-stream
 1.0
 (stream-map (lambda (guess)
 (sqrt-improve guess x))
 (sqrt-stream x))))