# SICP-1.3.1节练习

练习 1.29 - 1.33

练习 1.29:

Simpson's Rule is a more accurate method of numerical integration than the method illustrated above. Using Simpson's Rule, the integral of a function f between a and b is approximated as h - (y_0 + 4y_1 + 2y_2 + 4y_3 + 2y_4 + ... + 2y_(n-2) + 4y_(n-1) + y_n) 3 where h = (b - a)/n, for some even integer n, and y_k = f(a + kh). (Increasing n increases the accuracy of the approximation.) Define a procedure that takes as arguments f, a, b, and n and returns the value of the integral, computed using Simpson's Rule. Use your procedure to integrate cube between 0 and 1 (with n = 100 and n = 1000), and compare the results to those of the integral procedure shown above.

我的解答：

(define (simpson-integral f a b n) (define h (/ (- b a) n)) (define (y k) (f (+ a (* k h)))) (define (term k) (cond ((or (= k 0) (= k n)) (y k)) ((even? k) (* 2 (y k))) (else (* 4 (y k))))) (* (/ h 3.0) (sum term a 1+ b))) 运行： scheme@(guile-user)> (simpson-integral cube 0 1 100) 0.25 scheme@(guile-user)> (simpson-integral cube 0 1 1000) 0.25

练习 1.30:

The sum procedure above generates a linear recursion. The procedure can be rewritten so that the sum is performed iteratively. Show how to do this by filling in the missing expressions in the following definition: (define (sum term a next b) (define (iter a result) (if <??> <??> (iter <??> <??>))) (iter <??> <??>))

我的解答：

(define (sum term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ result (term a))))) (iter a 0))

练习 1.31:

The sum procedure is only the simplest of a vast number of similar abstractions that can be captured as higher-order procedures.3 Write an analogous procedure called product that returns the product of the values of a function at points over a given range. Show how to define factorial in terms of product. Also use product to compute approximations to [pi] using the formula4 pi 2 * 4 * 4 * 6 * 6 * 8 ... -- = ------------------------- 4 3 * 3 * 5 * 5 * 7 * 7 ... If your product procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process.

我的解答：

a: (define (produce term a next b) (if (> a b) 1 (* (term a) (produce term (next a) next b)))) (define (factorial n) (produce identity 1 1+ n)) (define (pi-produce n) (define (pi-term n) (/ (* (- n 1) (+ n 1)) (* n n))) (define (pi-next n) (+ n 2)) (* 4 (produce pi-term 3 pi-next n))) b: (define (produce term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* result (term a))))) (iter a 1))

练习 1.32:

Show that sum and product (Exercise 1-31) are both special cases of a still more general notion called accumulate that combines a collection of terms, using some general accumulation function: (accumulate combiner null-value term a next b) Accumulate takes as arguments the same term and range specifications as sum and product, together with a combiner procedure (of two arguments) that specifies how the current term is to be combined with the accumulation of the preceding terms and a null-value that specifies what base value to use when the terms run out. Write accumulate and show how sum and product can both be defined as simple calls to accumulate. If your accumulate procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process.

我的解答：

a. (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 + 0 term a next b)) (define (produce term a next b) (accumulate * 1 term a next b)) b. (define (accumulate combiner null-value term a next b) (define (iter acc count) (if (> count b) acc (iter (combiner acc (term count)) (next count)))) (iter null-value a))

练习 1.33:

You can obtain an even more general version of accumulate (Exercise 1-32) by introducing the notion of a filter on the terms to be combined. That is, combine only those terms derived from values in the range that satisfy a specified condition. The resulting filtered-accumulate abstraction takes the same arguments as accumulate, together with an additional predicate of one argument that specifies the filter. Write filtered-accumulate as a procedure. Show how to express the following using filtered-accumulate: the sum of the squares of the prime numbers in the interval a to b (assuming that you have a prime? predicate already written) the product of all the positive integers less than n that are relatively prime to n (i.e., all positive integers i < n such that GCD(i,n) = 1).

我的解答：

(define (filtered-accumulate filter combiner null-value term a next b) (cond ((> a b) null-value) ((filter a) (combiner (term a) (filtered-accumulate filter combiner null-value term (next a) next b))) (else (filtered-accumulate filter combiner null-value term (next a) next b)))) (define (sum-of-prime-squares a b) (filtered-accumulate prime? + 0 square 2 1+ 10)) (define (produce-of-relatively-primes n) (define (relatively-prime? i) (= (gcd i n))) (filtered-accumulate relatively-prime? * 1 identity 1 1+ (1- n)))

Feb 21, 2020 10:30:36 PM

Because name implies, deep cleanup represents another a higher level cleaning way up your office or house. Basic cleanup includes general clear like wiping along with vacuuming your kitchen, bathroom, bedroom and lounge floors, and also cleaning your cupboard entrance doors, tables, recliners, and such like, using normal water or some other basic cleanup products; on the other hand, deep cleaning is around paying attention to the facts and doing away with hidden airborne debris, dirt, along with small spots. It is often a top for you to bottom scrubbing in your home, from ceilings for you to floors, Instances of deep cleanup include cleanup what’s relating to the tiles, behind the appliance, under your sink, inside oven, cleaning the bedroom corners…etc.

Feb 22, 2020 06:17:10 PM

The third thing you'll want to check concerning before acquiring a carpet cleaners company might be they particular carpet vacuuming equipment not to mention methods construct y use given that they specify in any one single type from service. You should likewise ask whether they know about your specific particular rugs not to mention carpets given that they use the right manner of cleaning for what we should have. The third thing you are looking for is for a Oriental rugs and carpeting or Persian carpets to always be ruined web site company used unwanted carpet vacuuming process and / or chemicals.