Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[SICP] Ex 1.30 / 1.31

Name: Anonymous 2008-02-10 7:46

Here's what I have

; Ex 1.30
(define (sum term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ result (term a)))))
  (iter a 0))
; Ex 1.31
(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* result (term a)))))
  (iter a 1))

(define (fac n)
  (product (lambda (x) x) 1 (lambda (x) (+ x 1)) n))


Am i rite?

Name: Anonymous 2008-02-10 19:32

That's not general enough:

(define (curry f . curried)
  (lambda args
    (apply f (append curried args))))

(define (accumulate acc-function neutral end-test term from next end)
  (define (iter from result)
    (if (end-test from end)
        result
        (iter (next from) (acc-function result (term from)))))
  (iter from neutral))

(define sum (curry accumulate + 0 >))
(define product (curry accumulate * 1 >))



Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List