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

Deadly 提交于 2019-12-20 02:26:24

问题


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)

回答1:


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.

In the second case, you call procedure test, so the continuation is both

  1. cons 'wo to something
  2. call the procedure bound to saved (i.e. the continuation) with 'ca!

so the continuation calls itself, hence the loop.



来源:https://stackoverflow.com/questions/25339956/scheme-continuation-whats-the-difference-between-call-call-cc-in-top-level-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!