SICP 1.29-1.33体会

来源:互联网 发布:云计算平台有哪些 编辑:程序博客网 时间:2024/05/16 15:06

1.29 到1.33这5道题特别好。好在什么地方? 作者深入浅出的设计了一学习过程,特别适合学习和上手。

强烈建议大家都做做,特别精彩, 全部做完大概1-2小时,也不是很费时间。


下面是自己的作业:

1.29 

(define (sympson-integral f a b n)
    (define (sum term start next end)
(if (> start end)
   0
   (+ (term start)
      (sum term (next start) next end))))
  (define h (/ (- b a) n))
  (define (funcY index)
      (f (+ a (* index h))))
  (define (termY index)
      (cond ((= index 0) (funcY index))
   ((= index n) (funcY index))
   ((= (remainder index 2) 1) (* 4 (funcY index)))
   (else (* 2 (funcY index)))))
  (define (next-index index)
      (+ index 1))
  (/ (* h (sum termY 0 next-index n))
     3))


1.30

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


1.31

(define (product term a next b)
    (define (iter a result)
(if (> a b)
   result
   (iter (next a) (* result (term a)))))
  (iter a 1.0))
(define (product-recursive term a next b)
    (if (> a b)
1.0
(* (term a) 
  (product-recursive term (next a) next b))))
(define (product-pi-term index)
    (/ (* (+ 2.0 (* index 2)) 
 (+ 4.0 (* index 2)))
       (square (+ 3.0 (* index 2)))))
(define (next a)
    (+ a 1))
(define (product-pi n)
    (product product-pi-term 0 next n))
(define (product-pi-recur n)
    (product-recursive product-pi-term 0 next n))


1.32

(define (accumulate-i 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-r combiner null-value term a next b)
    (if (> a b)
null-value
(combiner (term a) 
  (accumulate-r combiner null-value term (next a) next b))))


(define (product-combiner x y)
    (* x y))
(define (product-i term a next b)
    (accumulate-i product-combiner 1.0 term a next b))
(define (product-r term a next b)
    (accumulate-r product-combiner 1.0 term a next b))
(define (sum-combiner x y)
    (+ x y))
(define (sum-i term a next b)
    (accumulate-i sum-combiner 0 term a next b))
(define (sum-r term a next b)
    (accumulate-r sum-combiner 0 term a next b))
(define (product-pi-term index)
    (/ (* (+ 2.0 (* index 2)) 
 (+ 4.0 (* index 2)))
       (square (+ 3.0 (* index 2)))))
(define (next a)
    (+ a 1))
(define (product-pi-i n)
    (product-i product-pi-term 0 next n))


1.33

(define (filter-accumulate-i combiner null-value term a next b filter)
    (define (iter a result)
(if (> a b)
   result
   (if (filter a)
(iter (next a) (combiner result (term a)))
(iter (next a) result))))
  (iter a null-value))


(define (filter-accumulate-r combiner null-value term a next b filter)
    (define (recur a)
(if (> a b)
   null-value
   (if (filter a)
(combiner (term a) 
     (recur (next a)))
(recur (next a)))))
  (recur a))


(define (prime? n)
(= n (smallest-divisor n)))
(define (smallest-divisor n)
    (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))
  (find-divisor n 2))
(define (sum-prime-i a b)
    (define (next a)
(+ a 1))
  (define (combine x y)
      (+ x y))
  (define (term x)
      x)
  (filter-accumulate-i combine 0 term a next b prime?))
(define (sum-prime-r a b)
    (define (next a)
(+ a 1))
  (define (combine x y)
      (+ x y))
  (define (term x)
      x)
  (filter-accumulate-r combine 0 term a next b prime?))


(define (gcd a b)
    (if (= b 0)
a
(gcd b (remainder a b))))


(define (sum-gcd-n-i n)
    (define (next x)
(+ 1 x))
  (define (combine x y)
      (* x y))
  (define null-value 1.0)
  (define (term x)
      x)
  (define (gcd-filter x)
      (= (gcd x n) 1))
  (filter-accumulate-i combine null-value term 2 next n gcd-filter))
(define (sum-gcd-n-r n)
    (define (next x)
(+ 1 x))
  (define (combine x y)
      (* x y))
  (define null-value 1.0)
  (define (term x)
      x)
  (define (gcd-filter x)
      (= (gcd x n) 1))
  (filter-accumulate-r combine null-value term 2 next n gcd-filter))

0 0