`
zhangle
  • 浏览: 25955 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

sicp 习题 1.29 ~ 1.33

    博客分类:
  • SICP
阅读更多
1.29
(define (inc n) (+ n 1))

(define (cube a) (* a a a))

(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))

(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2.0)) add-dx b)
     dx))

(define (simpson f a b n)
  (define h (/ (- b a) n))
  (define (y k) (f (+ a (* k h))))
  (define (next x) (+ x 1))
  (define (term i)
    (+ (y (- (* 2 i) 2))
       (y (* 2 i))
       (* 4 (y (- (* 2 i) 1)))))
  (/ (* h (sum term 1 next (/ n 2))) 3))

(define (simpson1 f a b n)
  (define (get-h) (/ (- b a) n))
  (define (get-y k) (f (+ a (* k (get-h)))))
  (define (simpson-term k)
    (cond ((= k 0) (get-y k))
          ((= k n) (get-y k))
          ((= (remainder k 2) 0) (* 2.0 (get-y k)))
          (else (* 4.0 (get-y k)))))
  (define (simpson-next k) (+ k 1))
  (* (/ (get-h) 3.0) (sum simpson-term 0 simpson-next n)))

(sum-cubes 1 10)

(integral cube 0 1 0.01)
(simpson cube 0.0 1.0 100)
(simpson1 cube 0.0 1.0 100)

(integral cube 0 1 0.001)
(simpson cube 0.0 1.0 1000)
(simpson1 cube 0.0 1.0 1000)


1.30
(define (inc n) (+ n 1))

(define (addself n) (+ (* 2 n) 1))

(define (cube a) (* a a a))

(define (f a b result)
  (cond ((< a b) (f (+ a 1) b (+ a result)))
        ((= a b) (+ a result))))

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

(f 1 3 0)

(sum cube 1 inc 10)


1.31
(define (inc n) (+ n 1))

(define (cube a) (* a a a))

(define (product term a next b)
  (if (> a b)
      1
      (* (term a)
         (product term (next a) next b))))

(define (product-iter term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* result (term a)))))
  (iter a 1))

(define (product-cubes a b)
  (product cube a inc b))

(define (product-cubes-iter a b)
  (product-iter cube a inc b))

(product-cubes 1 3)
(product-cubes-iter 1 3)

(define (double-even n)
  (* 4 n n))

(define (double-odd n)
  (+ (- (* 4 n n) (* 4 n)) 1))

(define (mypi n)
  (* (/ (product double-even 1.0 inc n) (* (product double-odd 1.0 inc n) (* 2 n))) 2))

(mypi 80.0)


1.32
(define (inc n) (+ n 1))

(define (cube a) (* a a 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 (accumulate-iter 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 (sum term a next b)
  (accumulate + 0 term a next b))

(define (sum-iter term a next b)
  (accumulate-iter + 0 term a next b))

(define (sum-cubes a b)
  (sum cube a inc b))

(define (sum-cubes-iter a b)
  (sum-iter cube a inc b))

(sum-cubes 1 10)
(sum-cubes-iter 1 10)


1.33
(define (square n)
  (* n n))

(define (smallest-divisor n)
  (find-divisor n 2))

(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 (divides? a b)
  (= (remainder b a) 0))

(define (prime? n)
  (= n (smallest-divisor n)))

(define (plain n) n)

(define (inc n) (+ n 1))

(define (filtered-accumulate filtered combiner null-value term a next b)
  (if (> a b)
      null-value
      (if (filtered a)
          (combiner (term a) (filtered-accumulate filtered combiner null-value term (next a) next b))
          (filtered-accumulate filtered combiner null-value term (+ a 1) next b))))

(define (sum term a next b)
  (filtered-accumulate prime? + 0 term a next b))

(define (sum-plain a b)
  (sum plain a inc b))

(sum-plain 1 10)

(define (product-prime n)
  (product plain 1 inc n))

(define (product term a next b)
  (define (co-prime? i)
    (if (and (= (gcd i b) 1) (< i b))
        #t
        #f))
  (filtered-accumulate co-prime? * 1 term a next b))

(product-prime 10)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics