You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
768 B
36 lines
768 B
(define (cube x) |
|
(* x x x)) |
|
|
|
(define (inc x) |
|
(+ x 1)) |
|
|
|
(define (identity x) x) |
|
|
|
(define (accumulate combiner null-value term a next b) |
|
(define (iter a result) |
|
(if (> a b) |
|
result |
|
(iter (next a) (combiner result (term a))))) |
|
(iter a null-value)) |
|
|
|
(define (accumulate-rec combiner null-value term a next b) |
|
(if (> a b) |
|
null-value |
|
(combiner (term a) |
|
(accumulate-rec term (next a) next b)))) |
|
|
|
(define (sum term a next b) |
|
(accumulate + 0 term a next b)) |
|
|
|
(define (product term a next b) |
|
(accumulate * 1 term a next b)) |
|
|
|
(define (factorial n) |
|
(product identity 1 inc n)) |
|
|
|
(define (nice-pi n) |
|
(define (pi-term n) |
|
(if (even? n) |
|
(/ (+ n 2) (+ n 1)) |
|
(/ (+ n 1) (+ n 2)))) |
|
(* (product pi-term 1 inc n) 4))
|
|
|