4

I don't understand how the following does not set up an infinite loop:

(define call/cc call-with-current-continuation) ; ccc alias
(define return #f) ; declare a global variable 'return'

(+ 1 (call/cc (lambda (cont) ; setup continuation with 'cont' as the exit procedure
                (set! return cont) ; set global var 'return' to the exit procedure
                1))) 

(return 22) ; 23

When I call (return 22), I jump back to the continuation, but with the passed value, 22, as the new evaluated result of the call/cc form. Would that not result in (return 22) being evaluated as the next statement, thus setting up an infinite loop?

I know it is not an infinite loop, but I do not understand why it is not.

SquareCrow
  • 291
  • 2
  • 14
  • see if [this](http://stackoverflow.com/questions/16529475/scheme-continuations-for-dummies/16531429#16531429) helps. – Will Ness Dec 12 '14 at 01:08

1 Answers1

4

It is an infinite loop. However, in most Scheme implementations, top-level forms are evaluated in prompts.

If you put your expressions inside a (let () ...), say, you'll definitely see the infinite loop in effect.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435