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 5

第二十三课

Previous函数式编程 - Scheme 4NextPython 基础

Last updated 6 years ago

Memory Model by Example

例1:

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

内存示意图:

例2:

有两种方式可以输出 ((1 2) 1 2),但它们的内存模型不一样:

> (cons '(1 2) '(1 2))
((1 2) 1 2)
> ((lambda (x) (cons x x)) '(1 2))
((1 2) 1 2)

variable number of arguments

之前实现过一次 map,接受一个 unary procedure 和一个 list,输出一个 list:

> (map car '((1 2) (3 4) (5 6 7)))
(1 3 5)

我们称这种 map 为 unary map 实现如下:

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

但不支持接受一个多元 procedure 和相应多个 lists,输出一个 list:

> (map + '(1 2) '(10 20) '(100 400))
(111 422)
> (map * '(1) '(2) '(3) '(4) '(5))
(120)

为了实现这种 general-map,我们首先需要能够定义接收任意数量参数的 procedure,即 varargs 语言特性:

> (define (bar a b c . d)
    (list a b c d))

> (bar 1 2 3 4 5 6)
(1 2 3 (4 5 6))

实现 map

(define (map fn first-list . other-lists)
  (if (null? first-list) '()
    (cons (apply fn 
            (cons (car first-list)
                  (unary-map car other-lists)))
          (apply map
            (cons fn
              (cons (cdr first-list)
                    (unary-map cdr other-lists)))))))

Garbage Collection

当我们在 Scheme 的 REPL 中输入:

> (cons "hello" '("world"))
("hello" "world")

Scheme 会在内部分配对应的内存,存储 hello 和 world 的指针,以及相应的字符串,在 REPL 打印完毕结果后,这些内存就会被相应地回收。回收的过程大致可以理解成一个递归函数,首先回收 cons 的 car 指针,然后回收 cons 的 cdr 指针,最后回收 construct 本身,其中回收 cons 的 cdr 指针就是回收 cons 的过程,依次递归。

如果使用 define 表达式,这些内存就会被保留:

> (define x '(1 2 3))
> (define y (cdr x))
; ...
> (define x '(4 5))

当执行完最后一个 define 表达式后,x 指向 '(4 5),y 指向 '(2 3),而为 1 分配的内存没有被引用,到了一定时候,系统的 Garbage Collector 就会启动,回收 1。那么 Garbage Collector 如何判断哪些内存可以回收?

reference count (doesn't work)

最直观的想法就是为每块内存记录被引用的数量,当引用数量等于 0 时,则可以被回收。但因为 Scheme 支持 set-car!、set-cdr! 这种 mutation 操作,Scheme list 可能出现循环引用,出现一个内存闭环,这个闭环内的数据互相引用,但并没有被实际使用。因此 reference count 的方法无法回收所有不再被使用的内存。

mark and sweep

示意图如下所示:

其中 master cons set 存着当前所有被分配的内存,Symbol Set 存着所有环境中的变量。

mark and sweep 分为三个阶段:

  1. 把 master cons set 中的所有 cons 标记为可以被回收

  2. 遍历 Symbol Set 中的指针,将所有被指到的内存标记为不可被回收

  3. 回收所有被标记为可以被回收的内存块 (sweep)

参考

Stanford-CS107-lecture-23