鉴于identity 定义中如何定义这些“收集器”函数,调用
(identity xs col)
对于任何列表xs和一些“收集器”函数col,相当于调用
(col xs)
所以 same 列表将被“返回”,即传递给它的参数“收集器”/延续函数col。这解释了它的名字,identity,那么。
为了比较,reverse 可以编码为
(define reverse ; to be called as e.g. (reverse l display)
(lambda (l col)
(cond
((null? l) (col '())) ; a reversed empty list is empty
(else (reverse (cdr l) ; a reversed (cdr l) is newl --
(lambda (newl) ; what shall I do with it when it's ready?
(col ; append (car l) at its end and let col
(append newl ; deal with it!
(list (car l))))))))))
这种编程风格被称为continuation-passing style:每个函数都传递了一个“延续”,假设它将传递其余计算的结果,因此原始延续/收集器函数将传递给最终的结果。每个收集器的参数代表它将收到的未来“结果”,然后收集器函数本身指定如何处理它然后。
不要对术语感到困惑:这些函数不是 call/cc 函数捕获的“延续”,它们是普通的 Scheme 函数,代表“接下来要做什么”。
定义可以理解为
identity :
to transform a list xs
with a collector function col,
is
| to call (col xs) , if xs is empty, or
| to transform (cdr xs)
with a new collector function col2
such that
(col2 r) = (col (cons (car xs) r)) , otherwise.
(或者我们可以把它写成伪代码)
(identity list col) =
| empty? list -> (col list)
| match? list (x . xs) -> (identity xs col2)
where
(col2 r) = (col (cons x r))
col2 通过将(cons x r) 传递给前一个处理程序col 来处理其参数r。这意味着r 被转换为(cons x r),但不是作为一个值返回,而是输入col 以进行进一步处理。因此,我们通过将新值 (cons x r) 传递给前一个“收集器”来“返回”它。
一个示例调用,作为说明:
(identity (list 1 2 3) display)
= (identity (list 2 3) k1)
; k1 = (lambda (r1) (display (cons 1 r1))) = display ° {cons 1}
= (identity (list 3) k2)
; k2 = (lambda (r2) (k1 (cons 2 r2))) = k1 ° {cons 2}
= (identity (list ) k3)
; k3 = (lambda (r3) (k2 (cons 3 r3))) = k2 ° {cons 3}
= (k3 '()) ; (((display ° {cons 1}) ° {cons 2}) ° {cons 3}) []
= (k2 (cons 3 '())) ; ((display ° {cons 1}) ° {cons 2}) [3]
= (k1 (cons 2 (list 3))) ; (display ° {cons 1}) [2,3]
= (display (cons 1 (list 2 3))) ; display [1,2,3]
= (display (list 1 2 3))
更新:在我最近喜欢使用的模式匹配伪代码中,我们可以编写
identity [] col = col []
identity [a, ...d] col = identity d ( newl => col [a, ...newl] )
和
reverse [] col = col []
reverse [a, ...d] col = reverse d ( newl => col [...newl, a] )
希望它在视觉上非常明显,几乎不需要解释!