[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.15.3 Continuation

Function: call-with-current-continuation proc
Function: call/cc proc
[R5RS] Encapsulates the current continuation to a procedure ("continuation procedure"), and calls proc with it. When proc returns, its value becomes call/cc's value. When the continuation procedure is invoked with zero or more arguments somewhere, the further calculation is abandoned and call/cc returns with the arguments given to the continuation procedure.

First class continuation is one of the most distinct feature of Scheme, but this margin is too small to contain explanation. Please consult to the appropriate documents.

Gauche supports full continuation, with a few limitations in rare cases. Normally a continuation has an unlimited extent. However, if a continuation is created during "callback" from C code--- that is, you call some C-implemented function that calls Scheme code again--the continuation's extent is limited until the Scheme evaluation returns to the C code. If you try to invoke the continuation from out of its extent, Gauche detects it and signals an error. This is a fundamental limitation and not likely to be addressed.

Note that it is still allowed to invoke a continuation from such callbacks. Also, the typical higher-order functions such as map, for-each or apply are not using callbacks, and not affected by this limitation

Fortunately, there are not much cases that you need to create an unlimited extent continuation in such callbacks. So far, the following code is executed in such callbacks. Besides them, typical callback functions from external C libraries, like GUI toolkit, obeys the same limitation.

(define *oob-cont* #f)

(call/cc (lambda (valid-cont)
           (sort '(1 2 3 4 5 6)
                 (lambda (a b)
                   (call/cc (lambda (oob-cont)
                              (set! *oob-cont* oob-cont)))
                   (valid-cont 'ok)))))
 => ok  ;callback can call a continuation

(*oob-cont* 'not-ok)
 => ;error -- *oob-cont* is out of its extent.

Function: dynamic-wind before thunk after
[R5RS] Before, thunk and after are all procedures with no arguments. In normal situation, dynamic-wind calls before, then thunk, then after, then returns whatever value(s) thunk returned.

If a control flow goes out from thunk by invoking a continuation captured outside of the dynamic scope of dynamic-wind (for example, an error is signalled in thunk), after is called.

If a control flow goes into thunk by invoking a continuation captured inside thunk from outside of the dynamic scope of dynamic-wind, before is called.
(letrec ((paths '())
         (c #f)
         (add (lambda (s) (push! paths s))))
   (lambda () (add 'connect))
   (lambda ()
     (add (call/cc (lambda (c0) (set! c c0) 'talk1))))
   (lambda () (add 'disconnect)))
  (if (< (length paths) 4)
      (c 'talk2)
      (reverse paths)))
 => (connect talk1 disconnect connect talk2 disconnect)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html