20-CS-4003-001 Organization of Programming Languages Fall 2017
Call-with-current-continuation examples

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code

Escaping from a deeply nested procedure call

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

(define halt (call/cc (lambda (k) k)))

(define proc
  (lambda () 
    (if (procedure? halt)
             (lambda (n)
               (if (= n 1000) 
                   (halt n) 
                   (looper (+ n 1))))))
          (looper 1))
 -  This is the same code as on the previous slide except that the continuation is "grabbed" by halt in a define. Thus the continuation is grabbed while the file is being loaded. When (proc) is executed the following results:
   ;Loading "callcc.1.ss"... done
   ;Value: halt
So, the 1000 is returned to the call/cc of the (define halt which sends control back in time to the load process which is why the puzzling "Loading" output is seen and since halt is the last value obtained in the code ;Value: halt is printed. At this point try
   prompt> halt
   ;Value: 1000
which shows that proc really did execute and iterate 1000 times.