Scheme Continuation: What's the difference bet

2020-04-11 06:40发布

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)

标签: scheme racket
1条回答
不美不萌又怎样
2楼-- · 2020-04-11 07:00

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.

查看更多
登录 后发表回答