Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning continuations from call/cc

Matt Might, in his posting on continuations by example, defines the helper function

(define (current-continuation)
  (call/cc (lambda (cc) (cc cc))))

to grab and return the current continuation. Why use (cc cc) to return the continuation? Why not use cc:

(define (current-continuation)
  (call/cc (lambda (cc) cc)))

Might's examples work identically with either version of current-continuation. Is this just a matter of style, or is there a deeper issue at work?

like image 570
r. clayton Avatar asked May 22 '26 19:05

r. clayton


1 Answers

So, just to remember call/cc in CPS is this:

(define (call/cc& f k)
  (f (lambda (v ignored-cont) (k v)) k))

So the first one does this:

(define (current-continuation k1)
  (call/cc& (lambda (cc k2) (cc cc k2)) k1))

The second one does this:

(define (current-continuation k1)
  (call/cc& (lambda (cc k2) (k2 cc)) k1))

Since call/cc will pass k1 as k2 and cc will use k1 both will end up doing (k1 cc). Thus there is no difference between the two.

like image 185
Sylwester Avatar answered May 25 '26 08:05

Sylwester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!