; Recursive version of accumulate
(define (accumulate a b combiner null-value term next)
(if (> a b)
null-value
(combiner (term a)
(accumulate (next a) b combiner null-value term next))))
; Iterative version of accumulate
(define (accumulate-iter a b combiner null-value term next)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner result
(term a)))))
(iter a null-value))
; implementation of sum func using accumulate
; (recursive accumulate)
(define (sum a b)
(accumulate a b + 0 (lambda (x) (x))
(lambda (x) (+ x 1))))
; implementation of product func using accumulate
; (iterative accumulate)
(define (product a b)
(accumulate-iter a b * 1 (lambda (x) (x))
(lambda (x) (+ x 1))))
; sum all naturals in [1..10] (recursive accumulate)
(sum 1 10)
(newline)
; multiply all naturals in [1..10] (recursive accumulate)
(product 1 10)
(newline)
OyBSZWN1cnNpdmUgdmVyc2lvbiBvZiBhY2N1bXVsYXRlCihkZWZpbmUgKGFjY3VtdWxhdGUgYSBiIGNvbWJpbmVyIG51bGwtdmFsdWUgdGVybSBuZXh0KQoJKGlmICg+IGEgYikKCQludWxsLXZhbHVlCgkJKGNvbWJpbmVyICh0ZXJtIGEpCgkJCSAgKGFjY3VtdWxhdGUgKG5leHQgYSkgYiBjb21iaW5lciBudWxsLXZhbHVlIHRlcm0gbmV4dCkpKSkKCjsgSXRlcmF0aXZlIHZlcnNpb24gb2YgYWNjdW11bGF0ZQooZGVmaW5lIChhY2N1bXVsYXRlLWl0ZXIgYSBiIGNvbWJpbmVyIG51bGwtdmFsdWUgdGVybSBuZXh0KQoJCShkZWZpbmUgKGl0ZXIgYSByZXN1bHQpCgkJCShpZiAoPiBhIGIpCgkJCQlyZXN1bHQKCQkJCShpdGVyIChuZXh0IGEpIChjb21iaW5lciByZXN1bHQKCQkJCQkJCSAodGVybSBhKSkpKSkKCQkoaXRlciBhIG51bGwtdmFsdWUpKQoKOyBpbXBsZW1lbnRhdGlvbiBvZiBzdW0gZnVuYyB1c2luZyBhY2N1bXVsYXRlCjsgKHJlY3Vyc2l2ZSBhY2N1bXVsYXRlKQooZGVmaW5lIChzdW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