R4RS 6.9  (call-with-current-continuation procedure)  ==>  object
S9 LIB    (call/cc procedure)                         ==>  object

PROCEDURE must be a procedure of one argument. The procedure
CALL-WITH-CURRENT-CONTINUATION packages up the current continuation
(see the rationale below) as an "escape procedure" and passes
it as an argument to PROCEDURE. The escape procedure is a Scheme
procedure of one argument that, if it is later passed a value,
will ignore whatever continuation is in effect at that later
time and will give the value instead to the continuation that
was in effect when the escape procedure was created.

The escape procedure that is passed to PROCEDURE has unlimited
extent just like any other procedure in Scheme. It may be stored
in variables or data structures and may be called as many times
as desired.

The following examples show only the most common uses of
CALL-WITH-CURRENT-CONTINUATION. If all real programs were as
simple as these examples, there would be no need for a procedure
with the power of CALL-WITH-CURRENT-CONTINUATION.

(call-with-current-continuation
  (lambda (exit)
    (for-each (lambda (x)
                (if (negative? x)
                    (exit x)))
              '(54 0 37 -3 245 19))
    #t))                               ==>  -3

(define list-length
  (lambda (obj)
    (call-with-current-continuation
      (lambda (return)
        (letrec ((r (lambda (obj)
                      (cond ((null? obj)
                              0)
                            ((pair? obj)
                              (+ (r (cdr obj)) 1))
                            (else
                              (return #f))))))
          (r obj))))))

(list-length '(1 2 3 4))               ==>  4
(list-length '(a b . c))               ==>  #f

Rationale:

A common use of CALL-WITH-CURRENT-CONTINUATION is for structured,
non-local exits from loops or procedure bodies, but in fact
CALL-WITH-CURRENT-CONTINUATION is extremely useful for implementing
a wide variety of advanced control structures.

Whenever a Scheme expression is evaluated there is a
continuation wanting the result of the expression. The
continuation represents an entire (default) future for the
computation. If the expression is evaluated at top level,
for example, then the continuation might take the result,
print it on the screen, prompt for the next input, evaluate
it, and so on forever. Most of the time the continuation
includes actions specified by user code, as in a continuation
that will take the result, multiply it by the value stored
in a local variable, add seven, and give the answer to the
top level continuation to be printed. Normally these
ubiquitous continuations are hidden behind the scenes and
programmers don't think much about them. On rare occasions,
however, a programmer may need to deal with continuations
explicitly. CALL-WITH-CURRENT-CONTINUATION allows Scheme
programmers to do that by creating a procedure that acts
just like the current continuation.

Most programming languages incorporate one or more special-purpose
escape constructs with names like exit, return, or even goto. In
1965, however, Peter Landin invented a general purpose escape
operator called the J-operator. John Reynolds described a simpler
but equally powerful construct in 1972. The catch special form
described by Sussman and Steele in the 1975 report on Scheme is
exactly the same as Reynolds's construct, though its name came from
a less general construct in MacLisp. Several Scheme implementors
noticed that the full power of the catch construct could be provided
by a procedure instead of by a special syntactic construct, and the
name CALL-WITH-CURRENT-CONTINUATION was coined in 1982. This name
is descriptive, but opinions differ on the merits of such a long
name, and some people use the name CALL/CC instead.

S9fES defines CALL/CC as an abbreviation for
CALL-WITH-CURRENT-CONTINUATION.
