SICP 1.29-1.33体会

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"SICP中文版"是指计算机科学经典教材《Structure and Interpretation of Computer Programs》(计算机程序的构造和解释)的中文翻译PDF版。这本教材由麻省理工学院的Harold Abelson和Gerald Jay Sussman等人编写,是计算机科学领域中一本重要的教材。 "SICP中文版PDF"提供了更方便的学习方式。无论是学生、程序员还是计算机科学爱好者,都可以在任何时候通过电子设备访问和学习这本教材。使用PDF格式的好处是可以在不同的平台上都能打开和阅读,而不受限于特定的操作系统或设备。 通过"SICP中文版PDF",读者可以学习计算机科学的基本原理和概念,如过程、数据抽象、递归、高阶函数、并发等。这本教材以Scheme语言为示例,帮助读者理解计算机程序的结构、设计和解释。通过逐步的案例和练习,读者可以锻炼解决问题和编写高质量代码的能力。 "SICP中文版PDF"也提供了沟通和讨论的平台。读者可以通过在线社群或论坛,与其他人分享学习心得、解答疑问和参与讨论。这为读者提供了一个学习交流的机会,促进了学习者之间的互动和共同成长。 总之,"SICP中文版PDF"是一本经典的计算机科学教材的中文翻译版本,使得更多的读者可以方便地学习和掌握其中的知识。无论是对于计算机科学专业的学生还是对计算机科学感兴趣的人,这本教材都是一本很好的参考书,并提供了丰富的实例和练习,让读者深入理解计算机程序的核心概念和设计原则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值