[英](call/cc): What exactly is continuation?
This question have been asked several times on SO, but none of them solve my question. 在SO上已经问过几次这个问题,但是没有一个解决我的问题。 What is continuation?
什么是延续?
Consider the following code: 考虑以下代码:
( (lambda (pair)
(begin (print (car pair))
((cdr pair) (cons (+ 1 (car pair))
(cdr pair)))))
(call/cc (lambda (k)
(cons 0 k))))
This program loops indefinitely printing the sequence of integers starting from 0. 该程序无限循环打印从0开始的整数序列。
What I understand is: 我的理解是:
Step 1: (call/cc (lambda (k) (cons 0 k)))
is evaluated, return pair (0 . #continuation)
(What is #continuation
in here) 步骤1:
(call/cc (lambda (k) (cons 0 k)))
被求值,返回对(0 . #continuation)
(这里的#continuation
)
Step 2: Apply the pair from step 1 to a lambda function. 步骤2:将步骤1中的配对应用于lambda函数。 The lambda function first print out the
car
, which is 0. lambda函数首先打印出
car
,即0。
Now, I am totally lost . 现在,我完全迷路了 。 The program is evaluating
(#continuation (1 . #continuation))
, which does not really make sense to me. 程序正在评估
(#continuation (1 . #continuation))
,这对我来说真的没有意义。 Is #continuation
a procedure now? #continuation
现在是程序吗?
What makes the program keep applying (call/cc (lambda (k) (cons 0 k)))
to the lambda function? 是什么让程序继续将
(call/cc (lambda (k) (cons 0 k)))
应用于lambda函数? So that it can keep calling itself 这样它就可以继续自称
I think I have figured it out. 我想我已经知道了。
Continuation is the current state of the program 继续是程序的当前状态
Given this, first we need to ask: what is the continuation, or the current state of this program? 鉴于此,我们首先要问:程序的延续是什么,还是当前状态是什么?
It is a lambda function waiting for a pair. 它是一个等待对的lambda函数。
#continuation = ( lambda-function <placeholder>)
<placeholder>
would be whatever we will pass to that lambda function, but we have not done that yet. <placeholder>
将成为我们将传递给该lambda函数的函数,但我们尚未这样做。
Now our program start to run, it evaluates the lambda function, then it evaluate the pair return by call/cc, which is (0 . #continuation)
. 现在我们的程序开始运行,它评估lambda函数,然后通过call / cc评估对返回,即
(0 . #continuation)
。
(print (car pair))
=> Next the lambda function prints out 0 (print (car pair))
=>接下来,lambda函数打印出0
((cdr pair) (cons (+ 1 (car pair)) (cdr pair)))))
=> Here, (cdr pair)
is our continuation
, which is (lambda-function <placeholder>)
, then our program pass a new pair into it. ((cdr pair) (cons (+ 1 (car pair)) (cdr pair)))))
=>这里, (cdr pair)
是我们的continuation
,即(lambda-function <placeholder>)
,然后我们的程序通过一对新的。 Then the program starts its infinite loop 然后程序开始其无限循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.