上次提供了递归的累加和模板,这次打算使用迭代的方法,空间复杂度会低一些
(define (sum term next a b)
(define (liter ans j)
(if(= j b)
ans
(liter (+ ans (term (next j))) (next j))))
(liter (term a) a))
一个具体的实现实例是
整数的累加和
(define (sum-int a b)
(define (term x) x)
(define (next x) (+ x 1))
(sum term next a b))