diff options
Diffstat (limited to 'coding-exercises/1/31.rkt')
| -rw-r--r-- | coding-exercises/1/31.rkt | 23 |
1 files changed, 6 insertions, 17 deletions
diff --git a/coding-exercises/1/31.rkt b/coding-exercises/1/31.rkt index 10f52c5..18b5170 100644 --- a/coding-exercises/1/31.rkt +++ b/coding-exercises/1/31.rkt @@ -1,26 +1,15 @@ #lang racket +(require "../../shared/chapter1.rkt") (require sicp) -(define (id x) x) - (define (factorial n) - (product id 1 inc n)) - -(define (product term a next b) - (define (iter result a) - (if (> a b) - result - (iter (* (term a) result) (next a)))) - (iter 1 a)) - -(define (even? x) - (= (remainder x 2) 0)) + (product (lambda (x) x) 1 inc n)) (define (pi-product n) - (/ (product (lambda (x) - (if (even? x) - x - (+ x 1))) + (/ (product (lambda (x) + (if (even? x) + x + (+ x 1))) 2 inc n) (product (lambda (x) (if (even? x) |
