sicp/zapiski/2_g1smo.scm

25 lines
526 B
Scheme

(define (sum-rec term a next b)
(if (> a b)
0
(+ (term a)
(sum-rec term (next a) next b))))
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (cube n) (* n n n))
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum cube a inc b))
(define (sum-cubes-rec a b)
(sum-rec cube a inc b))
(display (sum-cubes-rec 1 10))
(display "\n")
(display (sum-cubes 1 10))
(display "\n")