标签:term 方式 test fine ext bsp sum res 递归
(define (sum term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ (term a) result)))) (iter a 0)) ;test (sum (lambda (x) x) 1 (lambda (i) (+ i 1)) 10)
为什么要把iter塞到sum里面去:
(define (sum term a next b) (iter term a next b 0)) (define (iter term a next b result) (if (> a b) result (iter term (next a) next b (+ (term a) result))))
嗯,为了减少通信开销.
回头看递归写法:
(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))
"我们要以困难的方式搞定它!"
标签:term 方式 test fine ext bsp sum res 递归
原文地址:http://www.cnblogs.com/R4mble/p/7892764.html