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. Stanford-CS107

函数式编程 - Scheme 1

第十九课

引入

本课之前,我们已经介绍两种编程范式,面向过程 (procedure-oriented) 和面向对象 (object-oriented)。面向过程范式下的程序通常由几个高级 (high-level) 函数如 init, doTaskA, doTaskB, finish和一些负责更小过程的低级 (low-level) 函数构成;面向对象范式下的程序通常围绕着系统中的对象,通过对象之间的信息传递改变对象内部状态,从而完成具体编程任务。本节将介绍一种新的范式,函数式编程 (Functional Programming)。

函数式编程与面向过程编程有些相似,但最大的区别在于:面向过程编程不在乎每个函数的返回结果,而函数式编程则是围绕着每个函数的返回结果来编程。函数式编程通常把一个大的编程任务看作是一个函数,任务的输入数据就是函数的输入,而任务的结果就是函数的输出结果,而这个任务对应的函数又可以分解 (decompose) 成许多更小的函数,它们之间同样依靠输入和输出结果来沟通,达到完成最终任务的目的,举例如下:

f(x, y) = x^3 + y^2 + 7
g(x) = f(x, x+1) + 8
h(x, y, z) = f(x, z) * g(x+y)

我们的任务是就是输入 x, y, z,输出 h(x, y, z) 的值,而 h(x, y, z) 又可以被分解成 f(x, z) 和 g(x+y) 相乘的结果...

Scheme 举例

> (define celsius->fahrenheit (temp)
    (+ 32 (* 1.8 temp)))
> (celsius->fahrenheit 100)
212

为了和面向过程编程区分,我们把 celsius-> fahrenheit 称为 procedure,(celsius->fahrenheit 100) 称为 evaluate procedure,与二者对应的是面向过程编程中的函数 (function) 以及函数调用 (function call)。

Scheme 基础

Scheme 中,除了 primitive types,剩下的都是 list。这里 ' 实际上是 (quote ...) 的简写,它的作用是抑制 (suppress) evaluation,' 中的元素都是 list 数据。car 和 cdr 是 list 的选择器,car 返回 list 中的第一个元素,cdr 返回 list 中除第一个元素之外的 list。

> (car '(1 2 3 4 5))
1
> (cdr '(1 2 3 4 5))
(2 3 4 5)
> (car (cdr (cdr '(1 2 3 4 5))))
3
> (caddr '(1 2 3 4 5))
3
> (cdr '(4))
()
> (cdr '())
; specification 没有定义行为,不要这么做

cons 是 construct 的缩写,它可以理解为 car 的逆操作,将一个元素放到 list 的首位:

> (cons 1 '(2 3 4 5))
(1 2 3 4 5)
> (cons '(1 2 3) '(4 5))
((1 2 3) 4 5)

append 可以把两个 list 合成一个 list

> (append '(1 2 3) '(4 5)
(1 2 3 4 5)
> (append '(1 2) '(3) '(4 5) '(6 7 8))
(1 2 3 4 5 6 7 8)
> (append '(2 3) (list 1) '(4 5))

define 可以把一个名字和一个表达式在环境中联系起来

> (define add (x y)
    (+ x y))
ADD
> (add 10 7)
17

Scheme 在 runtime 中才会发现类型不匹配

> (add "hello" "world")
error

Scheme 中对整个 list 的操作通常选择用递归而不是迭代来完成

(define sum-of (numlist)
  (if (null? numlist) 0
      (+ (car numlist)
         (sum-of (cdr numlist))))

小结

Scheme 与 C 相比是一门对程序员来说更加简单的语言,它把程序员与内存直接打交道的工作完成,程序员可以更多地思考程序的逻辑本身。

参考

Previous复杂多线程问题Next函数式编程 - Scheme 2

Last updated 6 years ago

Stanford-CS107-lecture-19