185 lines
4.5 KiB
Scheme
185 lines
4.5 KiB
Scheme
; utils
|
|
(define (inc n) (+ n 1))
|
|
(define (cube n) (* n n n))
|
|
(define (square n) (* n n))
|
|
(define (identity n) n)
|
|
(define (even? n) (= (remainder n 2) 0))
|
|
(define (odd? n) (= (remainder n 2) 1))
|
|
(define (divides? a b) (= (remainder b a) 0))
|
|
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))
|
|
|
|
; copied prime? from 1.21
|
|
(define (find-divisor n test-divisor)
|
|
(cond ((> (square test-divisor) n) n)
|
|
((divides? test-divisor n) test-divisor)
|
|
(else (find-divisor n (+ test-divisor 1)))))
|
|
(define (smallest-divisor n)
|
|
(find-divisor n 2))
|
|
(define (prime? n) (if (= n 1) #f (= n (smallest-divisor n))))
|
|
|
|
(display "ex-1.29") (newline)
|
|
|
|
(define (sum term a next b)
|
|
(if (> a b)
|
|
0
|
|
(+ (term a)
|
|
(sum term (next a) next b))))
|
|
|
|
(define (sum-cubes a b)
|
|
(sum cube a inc b))
|
|
|
|
(display "(sum-cubes 1 10) = ")
|
|
(display (sum-cubes 1 10))
|
|
(newline)
|
|
|
|
(define (pi-sum a b)
|
|
(define (pi-term n) (/ 1.0 (* n (+ n 2))))
|
|
(define (pi-next n) (+ n 4))
|
|
(sum pi-term a pi-next b))
|
|
|
|
(display "(* 8 (pi-sum 1 100)) = ")
|
|
(display (* 8 (pi-sum 1 100)))
|
|
(newline)
|
|
|
|
|
|
(define (integral f a b dx)
|
|
(define (add-dx x) (+ x dx))
|
|
(* (sum f (+ a (/ dx 2.0)) add-dx b)
|
|
dx))
|
|
|
|
(display "(integral cube 0 1 0.01) = ")
|
|
(display (integral cube 0 1 0.01))
|
|
(newline)
|
|
|
|
(define (integral-simpson f a b n)
|
|
(define h (/ (- b a) n))
|
|
(define (y k) (f (+ a (* k h))))
|
|
(define (simpson-term k)
|
|
(cond ((= k 0) (y 0))
|
|
((= k n) (y k))
|
|
((even? k) (* 2 (y k)))
|
|
(else (* 4 (y k)))))
|
|
(* (/ h 3) (sum simpson-term 0 inc n)))
|
|
|
|
(display "(integral-simpson cube 0 1 100) = ")
|
|
(display (integral-simpson cube 0 1 100))
|
|
(newline)
|
|
|
|
(newline) (display "ex-1.30") (newline)
|
|
|
|
; maximum recursion dept exceed
|
|
(display (* 8 (pi-sum 1 1000000)))
|
|
|
|
(define (sum term a next b)
|
|
(define (sum-iter a acc)
|
|
(if (> a b)
|
|
acc
|
|
(sum-iter (next a) (+ acc (term a)))))
|
|
(sum-iter a 0))
|
|
|
|
(newline)
|
|
(display "(* 8 (pi-sum 1 1000000)) = ")
|
|
(display (* 8 (pi-sum 1 1000000)))
|
|
|
|
(newline)(newline) (display "ex-1.31") (newline)
|
|
(define (product term a next b)
|
|
(if (> a b)
|
|
1
|
|
(* (term a) (product term (next a) next b))))
|
|
|
|
(define (factorial n)
|
|
(product identity 1 inc n))
|
|
|
|
(display "(factorial 10) = ")
|
|
(display (factorial 10))
|
|
(newline)
|
|
|
|
(define (pi-product n)
|
|
(define (pi-term n) (/ (* n (+ n 2.)) (square (+ n 1.))))
|
|
(define (pi-next n) (+ n 2))
|
|
(product pi-term 2 pi-next n))
|
|
|
|
(display "(* 4 (pi-product 1000000)) = ")
|
|
(display (* 4 (pi-product 1000000)))
|
|
|
|
(define (product term a next b)
|
|
(define (product-iter term a next b acc)
|
|
(if (> a b)
|
|
acc
|
|
(product-iter term (next a) next b (* acc (term a)))))
|
|
(product-iter term a next b 1))
|
|
|
|
(newline)
|
|
(display "(* 4 (pi-product 1000000)) = ")
|
|
(display (* 4 (pi-product 1000000)))
|
|
|
|
(newline)(newline) (display "ex-1.32") (newline)
|
|
|
|
(define (mul-op a b) (* a b))
|
|
(define (sum-op a b) (+ a b))
|
|
|
|
; Recursive
|
|
(define (accumulate combiner null-value term a next b)
|
|
(if (> a b) null-value
|
|
(combiner (term a) (accumulate combiner null-value term (next a) next b))))
|
|
|
|
(define (sum term a next b)
|
|
(accumulate sum-op 0 term a next b))
|
|
|
|
(define (product term a next b)
|
|
(accumulate mul-op 1 term a next b))
|
|
|
|
(display (sum identity 1 inc 10)) (newline)
|
|
(display (product identity 1 inc 10)) (newline)
|
|
|
|
(display "(* 4 (pi-product 1000000)) = ")
|
|
(display (* 4 (pi-product 1000000)))
|
|
|
|
; Iterative
|
|
(define (accumulate combiner null-value term a next b)
|
|
(define (acc-iter a acc)
|
|
(if (> a b)
|
|
acc
|
|
(acc-iter (next a) (combiner acc (term a)))))
|
|
(acc-iter a null-value))
|
|
|
|
(newline)
|
|
(display "(* 4 (pi-product 1000000)) = ")
|
|
(display (* 4 (pi-product 1000000)))
|
|
|
|
|
|
(newline)(newline) (display "ex-1.33") (newline)
|
|
|
|
(define (filtered-accumulate combiner null-value filter term a next b)
|
|
(define (combiner-filtered acc a)
|
|
(if (filter a)
|
|
(combiner acc (term a))
|
|
acc))
|
|
(define (iter a acc)
|
|
(if (> a b)
|
|
acc
|
|
(iter (next a) (combiner-filtered acc a))))
|
|
(iter a null-value))
|
|
|
|
|
|
(define (primes-squared-sum a b)
|
|
(filtered-accumulate sum-op 0 prime? square a inc b))
|
|
|
|
(display (primes-squared-sum 1 5)) (newline) ; expected 38
|
|
|
|
(define (product-integers-coprime n)
|
|
(define (filter a) (= (gcd n a) 1))
|
|
(filtered-accumulate mul-op 1 filter identity 1 inc (- n 1)))
|
|
|
|
(display (product-integers-coprime 10)) (newline) ; expected 189
|
|
|
|
|
|
(newline) (display "ex-1.34 - see comment") (newline)
|
|
|
|
; Exercise 1.34. Suppose we define the procedure (define (f g) (g 2)). What
|
|
; happens if we (perversely) ask the interpreter to evaluate the combination
|
|
; (f ; f)? Explain.
|
|
|
|
; The result would be (2 2) and 2 is not applicable.
|
|
|