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

      

  • 相关阅读:
    iOS 列表三级展开
    iOS 聊天界面
    iOS 地图(添加大头针)
    iOS 地图
    swift 快速创建一些基本控件
    swift
    swift
    swift4.2 打印所有系统字体
    Xcode 去掉控制台无用打印信息
    swift
  • 原文地址:https://www.cnblogs.com/R4mble/p/7892680.html
Copyright © 2020-2023  润新知