25 lines
504 B
Scheme
25 lines
504 B
Scheme
#lang sicp
|
|
|
|
(define (product term a next b)
|
|
(define (iter a result)
|
|
(if (> a b)
|
|
result
|
|
(iter (next a) (* result (term a)))))
|
|
(iter a 1))
|
|
|
|
(define (pi-prod n)
|
|
(define (pi-term i)
|
|
(if (even? i)
|
|
(/ (+ i 2) (+ i 1))
|
|
(/ (+ i 1) (+ i 2))))
|
|
(product pi-term 1 inc n))
|
|
|
|
;(define (inc x) (+ x 1))
|
|
|
|
(define (factorial n)
|
|
(product identity 1 inc n))
|
|
|
|
(define (product-rec term a next b)
|
|
(if (> a b)
|
|
1
|
|
(* (term a) (product-rec term (next a) next b))))
|