open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  1. MIT - 6.001 - SICP

程序抽象

定义计算模式 - lambda 表达式

> (lambda (x) (* x x))
#<procedure>

lambda 表达式是 Scheme 中的一种特殊形式表达式,它返回的不是原始对象,不是名字与某对象的绑定,也不是组合,而是一个通用的计算模式 (common pattern of computation),也可以称为一段程序 (procedure)。在这段程序中,有参数 (parameters) 和程序体 (body)。我们称 Scheme 中原始环境中存在的许多程序段,称为原始程序 (primitive procedure),使用 lambda 表达式生成的程序段称为复合程序 (compound procedure)。

因此,在 Scheme 表达式的评价规则中,在执行程序段时,我们可以加入以下两条:

  1. 如果是原始程序,就按照其定义的方式执行。

  2. 如果是复合程序,那么我们将 lambda 函数体中的变量全部替换成输入的参数,然后对其进行评价。

举例如下:

> ((lambda (x) (* x x)) 5) ; evaluated as (* 5 5)
25

结合名字表达式的评价规则,我们可以将复合程序与某个名字绑定,避免每次重新输入复合程序:

> (define square (lambda (x) (* x x)))
> (square 5)
25

总结:lambda 表达式的评价值可以被理解为程序对象 (procedure object),它的内部存有对参数和函数体的表达,而这个程序对象在被应用时 (即:在组合表达式中),会将程序体重的参数替换成实际给定的参数,然后将函数体评价后的结果返回。

lambda 表达式与程序抽象

lambda 表达式本身即是程序抽象的描述,即描述获取 declarative knowledge 的步骤。如果这些步骤比较复杂,我们就需要将步骤进一步划分为多个模块 (module) 或者阶段 (stages)。这样做包括但不局限于以下两个好处:

  • 有些模块或阶段可以在其它程序中重复使用

  • 模块或阶段将程序的细节和程序的使用隔离,使用者无须了解具体实现。

举例如下:

; 利用勾股定理计算直角三角形斜边的长度

> (define square (lambda (x) (* x x)))
> (define sum-squares
    	(lambda (x y) (+ (square x) (square y))))
> (define pythagoras
    	(lambda (y x) (sqrt (sum-squares y x))))

抽象程序的过程,通常可以总结为一下 4 个步骤:

  1. 找出程序中的模块或阶段

  2. 利用程序抽象来描述这些模块或阶段

  3. 建立一个大的程序来联系模块或阶段之间的输入输出

  4. 在模块或阶段中递归地重复以上步骤

更复杂的例子 - 牛顿法求平方根

  1. 猜一个数,设为G

  2. 如果 G 和目标足够接近,停止

  3. 否则,更新猜测的值为 (G + X/G) / 2

判断是否足够接近的模块:

> (define squre
    (lambda (x) (* x x)))
> (define close-enuf?
    (lambda (guess x)
      (< (abs (- (square guess) x)) 0.001)))

更新猜测值的模块:

> (define average
    (lambda (a b) (/ (+ a b) 2)))

> (define improve
    (lambda (guess x)
      (average guess (/ x guess))))

整合整个程序:

> (define sqrt-loop (lambda G X)
  	(if (close-enuf? G X)
        G
        (sqrt-loop (improve G X) X)))

> (define sqrt
    (lambda (x)
      (sqrt-loop 1.0 x)))

参考

Previous什么是程序Next替代模型

Last updated 6 years ago

Youtube: MIT6.001-SICP-2004-lecture-2
MIT6.006-SICP-2005-lecture-notes-2