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-11 5:26

>>19
That is not general enough either.
With OPs code you can return the sum of all odd numbers 1..100 like this

(define (inc x) (+ x 1))
(define (id x) (x))

(sum (lambda (x) (if (odd? x) x 0)) 1 inc 100)

; or if you are sure a is odd and not even

(sum id 1 (lambda (x) (+ x 2)) 100)


You cannot do that with your procedures.

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