0

目前,我在函数式语言中试验continuation的时候,我的理解是一个continuation记录了当前的程序计数器和寄存器文件,当一个continuation返回时,PC和注册的文件就会恢复到它记录的值.

因此,在以下来自Might 博客文章的愚蠢示例中,

; right-now : -> moment
(define (right-now)
  (call-with-current-continuation 
   (lambda (cc) 
     (cc cc))))

; go-when : moment -> ...
(define (go-when then)
  (then then))  


; An infinite loop:
(let ((the-beginning (right-now)))
  (display "Hello, world!")
  (newline)
  (go-when the-beginning))  ; here the-beginning continuation passed to go-when, which ultimately will have an continuation applied to an continuation, that returns a continuation, which will cause the the program point resumed to the PC and registers states recorded in it.

我不确定我的理解是否正确..如果您认为不正确,请纠正我....

4

1 回答 1

8

程序计数器和寄存器文件不是延续记录的内容。

描述 call-with-current-continuation 含义的最佳方式是它记录了程序上下文。例如,假设您正在评估程序

(+ 3 (f (call-with-current-continuation g)))

在这种情况下,call-with-current-continuation 表达式的上下文将是

(+ 3 (f [hole]))

也就是说,围绕当前表达式的内容。

Call-with-current-continuation 捕获这些上下文之一。调用延续会导致将当前上下文替换为存储在延续中的上下文。

上下文的概念很像堆栈的概念,只是上下文中的函数调用没有什么特别之处。

这是一个非常简短的治疗。我强烈建议您查看 Shriram Krishnamurthi 的(免费,在线)书PLAI,尤其是第 VII 部分,以更详细和仔细地了解这个主题。

于 2011-03-28T19:56:00.560 回答