# CPS Lecture

`(f (g (h i) j) k)` 的哪一部分先被完成？ `(h i)` , 因为它必须在 `(g (h i) j)` 可以应用之前先完成求值。
`(f (g (h i) (j k)))` 呢？Scheme没有指定参数的求值顺序，因此可以是 `(h i)``(j k)`

``````(f (g (h i) (j l)))
``````

``````(h i (lambda (hi) (f (g hi (j l)))))
``````

`(lambda (hi) (f (g hi (j l))))` 就是一个连续(continuation)。`hi`在连续中只出现一次，因为hi的目的仅仅是替代 `(h i)`

``````(define rember8
(lambda (ls)
(cond
[(null? ls) '()]
[(= (car ls) 8) (cdr ls)]
[else (cons (car ls) (rember8 (cdr ls)))])))
``````

``````(lambda (x ...) ...)  =>  (lambda (x ... k) ...^)
``````

``````(define rember8
(lambda (ls k)
(cond
[(null? ls) '()]
[(= (car ls) 8) (cdr ls)]
[else (cons (car ls) (rember8 (cdr ls)))])))
``````

``````(define rember8
(lambda (ls k)
(cond
[(null? ls) (k '())]
[(= (car ls) 8) (cdr ls)]
[else (cons (car ls) (rember8 (cdr ls)))])))
``````

`cond` 的第二行同样有作为测试和返回值的小部件，所以我们如同第一行一样处理。

``````(define rember8
(lambda (ls k)
(cond
[(null? ls) (k '())]
[(= (car ls) 8) (k (cdr ls))]
[else (cons (car ls) (rember8 (cdr ls)))])))
``````

``````(define rember8
(lambda (ls k)
(cond
[(null? ls) (k '())]
[(= (car ls) 8) (k (cdr ls))]
[else (rember8 (cdr ls) (lambda (x) (cons (car ls) x)))])))
``````

``````(define rember8
(lambda (ls k)
(cond
[(null? ls) (k '())]
[(= (car ls) 8) (k (cdr ls))]
[else (rember8 (cdr ls) (lambda (x) (k (cons (car ls) x))))])))
``````

``````> (rember8 '(1 2 8 3 4 6 7 8 5) (lambda (x) x))
``````

``````(define rember8
(lambda (ls k)
(cond
[(null? ls) (*k* '())]
[(= (car ls) 8) (*k* (cdr ls))]
[else (*rember8* (cdr ls) (lambda (x) (*k* (cons (car ls) x))))])))
``````

``````ls                       |   k
'(1 2 8 3 4 6 7 8 5)   |   (lambda (x) x) = id
'(2 8 3 4 6 7 8 5)     |   (lambda (x) (id (cons 1 x))) = k2
'(8 3 4 6 7 8 5)       |   (lambda (x) (k2 (cons 2 x))) = k3
``````

``````(k3 '(3 4 6 7 8 5)) = (k2 (cons 2 '(3 4 6 7 8 5)))
(k2 '(2 3 4 6 7 8 5)) = (id (cons 1 '(2 3 4 6 7 8 5)))
(id '(1 2 3 4 6 7 8 5)) = '(1 2 3 4 6 7 8 5)
``````

``````(define multirember8
(lambda (ls)
(cond
[(null? ls) '()]
[(= (car ls) 8) (multirember8 (cdr ls)))]
[else (cons (car ls) (multirember8 (cdr ls)))])))
``````

``````(define multirember8
(lambda (ls k)
(cond
[(null? ls) (k '())]
[(= (car ls) 8) (multirember8 (cdr ls))] ;; uh-oh~
[else (multirember8 (cdr ls) (lambda (x) (k (cons (car ls) x))))])))
``````

``````(define multirember8
(lambda (ls k)
(cond
[(null? ls) (k '())]
[(= (car ls) 8) (multirember8 (cdr ls) (lambda (x) (k x)))]
[else (multirember8 (cdr ls) (lambda (x) (k (cons (car ls) x))))])))
``````

Eta规约： (lambda (x) (M x)) = M 如果x不是自由变量，且M保证可以终止。M是任意满足以上条件的表达式，不仅限于单个变量比如k。

### 推荐阅读更多精彩内容

• 栈 1. 栈（stack）又名堆栈，它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被...
程序员高级码农阅读 5,538评论 0 11
• 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
夏天再来阅读 2,132评论 0 2
• 你捧着那本书 一杯红茶和奶糖 时间总在小恬中，细细琢磨 一只烟，从点燃到燃尽 是孤独的，孤独如星 在沉寂久远的深夜...
羊与远方阅读 119评论 0 1
• 为了丰富车间干部职工的文化生活，缓解工作压力和疲劳，增进相互之间的感情，疏勒河信号车间于2018年6月23...
夏目岳七阅读 187评论 3 3
• 亲子时间管理践行D142 打卡日期：20180829 周三 第2个90天打卡累计天数：52/90 #宣言:让优秀成...
漫定投阅读 110评论 0 0
• 浑浑噩噩，终日迷荡。如我荒废者少矣，以至今日提笔手颤，字抖形异。心中抑郁，说与何人听？欲述父母，然其事杂而乱，...
即墨云星阅读 148评论 0 0
• 尹、庄二人再一次上路。这一次，他们来到一个叫做“红罗书院”的所在。 “这是哪里？”尹喜问道。 “我也不知道。”庄子...
原疯不动阅读 314评论 1 4