我们可以先写一个计算n次的过程,然后,double过程就是当n=2的情况。
(define (cube x) (* x x x))
(define (square x) (* x x))
(define (inc x) (+ 1 x))
(define (calculate-n-times n f)
(lambda (x)
(if (= n 1) (f x) (f ((calculate-n-times (- n 1) f) x)))))
(define (double f)
(calculate-n-times 2 f))
((double inc) 2)
((double square) 2)
((double cube) 2)
运行结果
4
16
512