read

[CODE]
; Recursive
(define (accumulate combiner null-value term a next b)
  (if (> a b)
      null-value
      (combiner (term a) (accumulate combiner null-value term (next a) next b))))

; Iterative
(define (accumulate combiner null-value term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (combiner result (term a)))))
  (iter a null-value))

; Sum
> (accumulate (lambda (x y) (+ x y)) 0 term 1 next 5)
15
; Product
> (accumulate (lambda (x y) (* x y)) 1 term 1 next 4)
24
[/CODE]

Blog Logo

Ki Sung Bae


Published

Image

Gsong's Blog

Developer + Entrepreneur = Entreveloper

Back to Overview