灯下 登录
计算机科学 / SICP / 1.3.4 Procedures as Returned Values

Exercise 1.43 · 习题

Exercise 1.43: If f is a numerical function
and n is a positive integer, then we can form the n

th repeated
application of f, which is defined to be the function whose value at x
is f
(
f
(

(
f
(
x
)
)

)
). For example, if f is the
function x

x
+
1, then the n

th repeated application of f is
the function x

x
+
n. If f is the operation of squaring a
number, then the n

th repeated application of f is the function that
raises its argument to the 2
n

-th power. Write a procedure that takes as
inputs a procedure that computes f and a positive integer n and returns
the procedure that computes the n

th repeated application of f. Your
procedure should be able to be used as follows:

((repeated square 2) 5)
625

Hint: You may find it convenient to use compose from Exercise 1.42.

练习 1.43:若 f 是一个数值函数,n 是正整数,则我们可以构造 f 的第 n 次重复施用,它定义为这样一个函数:其在 x 处的值为 f(f(…(f(x))…))。例如,若 f 是函数 x ↦ x + 1,则 f 的第 n 次重复施用是函数 x ↦ x + n;若 f 是对一个数求平方的运算,则 f 的第 n 次重复施用是将其参数提升到 2ⁿ 次方的函数。编写一个过程,以计算 f 的过程和正整数 n 为输入,返回计算 f 的第 n 次重复施用的过程。你的过程应能如下使用:

((repeated square 2) 5)
625

提示:你可能会发现使用练习 1.42 中的 compose 很方便。

Racket #lang sicp
((repeated square 2) 5)
625