some solutions for MIT Press's "structure and interpretation of computer programs", I guess
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.

37 lines
692 B

(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (identity x) x)
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))
(define (product-rec term a next b)
(if (> a b)
0
(* (term a)
(product-rec term (next 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))