简体   繁体   中英

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.

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 (ie the continuation) with 'ca!

so the continuation calls itself, hence the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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