zoukankan      html  css  js  c++  java
  • 1.3.1 (对过程的抽象)

    (define (sum-integers a b)
      (if (> a b)
          0
          (+ a (sum-integers (+ a 1) b))))
    
    (sum-integers 1 10)
    
    (define (sum-cubes a b)
      (define (cube x)
        (* x x x))
      (if (> a b)
          0
          (+ (cube a) (sum-cubes (+ a 1) b))))
    
    (sum-cubes 1 10)
    
    (define (pi-sum a b)
      (if (> a b)
          0
          (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))
    
    (* 8 (pi-sum 1 1000000))
    

      对过程的抽象:

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

      填充:

    (define (inc n)
      (+ n 1))
    
    (define (cube n)
      (* n n n))
    
    (define (sum-cubes a b)
      (sum cube a inc b))
    (sum-cubes 1 10)

      

    (define (inc n)
      (+ n 1))
    
    (define (identity x) x)
    
    (define (sum-integers a b)
      (sum identity a inc b))
    
    (sum-integers 1 10)
    

      

    (define (pi-next n)
      (+ n 4))
    
    (define (pi-term x)
      (/ 1.0 (* x (+ x 2))))
    
    (define (pi-sum a b)
      (sum pi-term a pi-next b))
    
    (* 8 (pi-sum 1 10000))
    

      拓展:

    (define (integral f a b dx)  ;积分
      (define (add-dx x) (+ x dx))
      (* (sum f (+ a (/ dx 2.0)) add-dx b)
         dx))
    
      (define (cube x) (* x x x))
      
    (integral cube 0 1 0.0001)
    

      

  • 相关阅读:
    数据分析三剑客numpy pandas Matplotlib
    算法 初识
    python 爬虫二
    python 爬虫一
    python celery
    elasticsearch 学习
    ansible 基本使用
    面试题
    奇技淫巧
    【前端基础】- CSS 1-CSS选择器
  • 原文地址:https://www.cnblogs.com/R4mble/p/7892680.html
Copyright © 2011-2022 走看看