简体   繁体   English

(通话/抄送):延续到底是什么?

[英](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.

相关问题 调用continuation vs函数调用 - Invoking a continuation vs a function call 当前延续的调用会忽略其自身的延续吗? - does call with current continuation ignore its own continuation? 可以使用“call / cc”实现“if”吗? - Can “if” be implemented using “call/cc”? 是否可以使用call / cc来实现递归? - Is it possible to use call/cc to implement recursion? 幺半群同态究竟是什么? - What is monoid homomorphism exactly? 究竟什么是局部推理? - What is Local Reasoning , exactly? 为什么我们可以实现call / cc,但是经典逻辑(直觉+ call / cc)不是建设性的? - How come that we can implement call/cc, but the classical logic (intuitionistic + call/cc) is not constructive? Promise优于CPS和Continuation Functor / Monad有什么优势? - What are the advantages of Promises over CPS and the Continuation Functor/Monad? 连续传递样式导致“超出最大调用堆栈大小”错误 - Continuation-passing style causes "Maximum call stack size exceeded " error Xcode 警告:非最终 class 'CustomMedication' 不能安全地符合协议 'CustomComponent',这要求 'Self.C.CC' 完全一致 - Xcode Warning: Non-final class 'CustomMedication' cannot safely conform to protocol 'CustomComponent', which requires that 'Self.C.CC' is exactly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM