Wednesday, June 3, 2009

Ex-1.31

;a
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))

;facrotial in terms of product function
(define (return-arg arg) arg)
(define (incf x) (+ x 1))
(define (factorial n)
(product return-arg 1 incf n))

;PI calculation - TODO

(define (add2 x) (+ x 2))
(* (square
(/ (product return-arg 2.0 add2 100.0)
(product return-arg 3.0 add2 101.0))) 4)

;b
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))

No comments:

Post a Comment