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 3

第二十一课

map

一种常见的操作就是将一个 sequence 转化成另一个 sequence,其中对 sequence 的每个元素都执行同样的操作,如:

> (double-all '(1 2 3 4))
(2 4 6 8)
> (incr-all '(1 2 3 4))
(2 3 4 5)

其实这就是我们在其它语言中常见的 map。在 Scheme 中,我们希望这样:

> (define (double x) (* x 2))
> (define (incr x) (+ x 1))
; map unary functions
> (map double '(1 2 3 4))
(2 4 6 8)
> (map incr '(1 2 3 4))
(2 3 4 5)
> (map car '((1 2) (4 8 2) (11)))
(1 4 11)
> (map cdr '((1 2) (4 8 2) (11)))
((2) (8 2) ())
; map binary functions
> (map cons '(1 2 8) '((4) () (2 5)))
((1 4) (2) (8 2 5))
> (map + '(1 2) '(3 4) '(6 10))
(10 16)

实现 map

(define (my-unary-map fn seq)
  (if (null? seq) '()
      (cons (fn (car seq)) (my-unary-map fn (cdr seq)))))

apply & eval

eval 接收一个参数,这个参数是程序员输入的原始表达式字符串,之后 eval 会 tokenize、parse 原始表达式字符串,然后再用 evaluator 来处理解析后的 list structure,最终得到输出:

>> (eval '(+ 1 2 3))
6

apply 总是接受两个参数,procedure 的 symbol 和 argument list,apply 会用 cons 把 symbol 和 argument list 合成一个 list 作为输入交给 eval 执行:

> (apply + '(1 2 3))
6
> (define (average num-list)
    (/ (apply + num-list)
       (length num-list)))
> (average '(1 2 3 4))
5/2

了解了 eval 和 apply 我们可以回顾一下 flatten

(define (flatten seq)
  (if (not (list? seq))
    (list seq)
    (apply append
      (map flatten seq))))

translate

> (define (translate points delta)
    (map (lambda (x) (+ x delta))
         points))

> (translate '(2 5 8 11 25) 100)
(102 105 108 111 125)

这里利用 lambda 在 translate procedure 内部定义 anonymous procedure,后者中的自由变量 delta 可以在它的外环境找到 binding。更直接地,我们也可以使用 named procedure:

(define (translate seq delta)
  (define (shift-by x)
    (+ x delta))
  (map shift-by seq))

define named procedure

(define (sum x y)
  (+ x y))

(define sum
  (lambda (x y) (+ x y)))

第一种写法实际上是第二种写法的 syntactic sugar。

参考

Previous函数式编程 - Scheme 2Next函数式编程 - Scheme 4

Last updated 6 years ago

Stanford-CS107-lecture-21