22 hours ago
my attempt to do the exercises in sicp.
Tuesday, July 1, 2008
sicp exercise 1.43
; Exercise 1.43. If f is a numerical function and n is a positive integer, then we can form the nth
; 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 nth repeated application of f is the function
; x -> x + n. If f is the operation of squaring a number, then the nth repeated application of f is the
; function that raises its argument to the 2^nth 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 nth repeated
; application of f. Your procedure should be able to be used as follows:
; ((repeated square 2) 5)
; Hint: You may find it convenient to use compose from exercise 1.42.
(define (square x) (* x x))
(define (compose f g)
(f (g x))))
(define (repeated f n)
(define (iter i res)
(if (>= i n)
(iter (+ i 1) (compose f res))))
(iter 1 f))
(display ((repeated square 2) 5)) (newline)