简体   繁体   中英

Continuation in Scheme

I think I got what a continuations is (in general), but I can't understand how it is used in Scheme.

Consider this example (from wikipedia call/cc )

(define (f return)
  (return 2)
  3)
(display (call/cc f)) ;=> 2

I cannot understand why:

  • the continuation is implicit?right?

  • How is the continuation in this case?

The continuation is the "rest of the computation" that remains to be executed. In your particular example, you could think of this as being (display []) where [] is a hole to be plugged with a value. That is, at the point that call/cc is invoked, what remains to be done is the call to display.

What call/cc does is take this continuation and puts it in a special value that can be applied like a function. It passes this value to its argument (here f ). In f , the continuation is bound to return . So (return 2) will basically plug 2 into the continuation, ie, (display 2) .

I don't think this example is actually very helpful, so I think you should read PLAI if you're interested in learning more about continuations (see Part VII). Another good source is these lecture notes by Dan Friedman.

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