简体   繁体   English

方案继续:顶层的“ call / cc”呼叫与非顶层的“ call / cc”呼叫有什么区别?

[英]Scheme Continuation: What's the difference between call 'call/cc' in top level and non-top level?

This code works as expected: 此代码按预期工作:

(define saved #f)
(cons 'wo (call/cc (lambda (k) (set! saved k) '())))
(saved 'ca!)

output (Racket console): 输出(球拍控制台):

'(wo)
'(wo . ca!)

But when I wrap it in a function and call it, the program never stops. 但是,当我将其包装到函数中并调用它时,该程序永远不会停止。 Why? 为什么?

(define (test)
    (define saved #f)
    (cons 'wo (call/cc (lambda (k) (set! saved k) '())))
    (saved 'ca!))

(test)

A continuation is all that's left to be done in the execution context where it's saved. 延续是保存在执行上下文中要做的所有事情。

In the first case, the continuation is saved when calling cons , so it's just to cons 'wo to something and return to the REPL. 在第一种情况下,在调用cons时保存了延续,因此,这仅是为了使cons'wo whats并返回REPL。

In the second case, you call procedure test , so the continuation is both 在第二种情况下,调用程序test ,所以延续既是

  1. cons 'wo to something 不利于某事
  2. call the procedure bound to saved (ie the continuation) with 'ca! 用“ ca”调用绑定到已saved (即继续)的过程!

so the continuation calls itself, hence the loop. 因此,延续会自我调用,从而产生循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM