灯下 登录
计算机科学 / SICP / 4.2.2 An Interpreter with Lazy Evaluation

Exercise 4.29 · 习题

Exercise 4.29: Exhibit a program that you would
expect to run much more slowly without memoization than with memoization.
Also, consider the following interaction, where the id procedure is
defined as in Exercise 4.27 and count starts at 0:

(define (square x) (* x x))

;;; L-Eval input:
(square (id 10))

;;; L-Eval value:
⟨response⟩

;;; L-Eval input:
count

;;; L-Eval value:
⟨response⟩

Give the responses both when the evaluator memoizes and when it does not.

练习 4.29:给出一个程序,你预期它在没有记忆化的情况下运行速度会远慢于有记忆化时。另外,考虑以下交互,其中 id 过程的定义同练习 4.27,count 初始为 0:

(define (square x) (* x x))

;;; L-Eval input:
(square (id 10))

;;; L-Eval value:
⟨response⟩

;;; L-Eval input:
count

;;; L-Eval value:
⟨response⟩

分别给出求值器有记忆化和无记忆化时的响应结果。

Racket #lang sicp
(define (square x) (* x x))

;;; L-Eval input:
(square (id 10))

;;; L-Eval value:
⟨response⟩

;;; L-Eval input:
count

;;; L-Eval value:
⟨response⟩