If is a numerical function and is a positive integer, then we can form the -th repeated application of , which is defined to be the function whose value at is .
For example, if is the function , then the -th repeated application of is the function .
If is the operation of squaring a number, then the -th repeated application of is the function that raises its argument to the -th power.
Write a procedure that takes as inputs a procedure that computes and a positive integer and returns the procedure that computes the -th repeated application of .
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.
Answer
(define (square x) (* x x))
(define (compose f g)
(λ (x) (f (g x))))
(define (repeated f n)
(if (= n 1)
(λ (x) (f x))
(repeated (compose f f) (- n 1))))
((repeated square 2) 5)