dynamic-wind -- r7rs Definition procedure;
dynamic-wind (from vonuvoli);Procedure variants:
(((before procedure-0) (thunk procedure-0) (after procedure-0)) -> (any))
before of type procedure-0;thunk of type procedure-0;after of type procedure-0;any;scheme:base -- (scheme base);scheme -- (scheme);(dynamic-wind before thunk after)Calls
thunkwithout arguments, returning the result(s) of this call.Beforeandafterare called, also without arguments, as required by the following rules. Note that, in the absence of calls to continuations captured usingcall-with-current-continuation, the three arguments are called once each, in order.Beforeis called whenever execution enters the dynamic extent of the call tothunkandafteris called whenever it exits that dynamic extent. The dynamic extent of a procedure call is the period between when the call is initiated and when it returns. Thebeforeandafterthunks are called in the same dynamic environment as the call todynamic-wind. In Scheme, because ofcall-with-current-continuation, the dynamic extent of a call is not always a single, connected time period. It is defined as follows:
The dynamic extent is entered when execution of the body of the called procedure begins.
The dynamic extent is also entered when execution is not within the dynamic extent and a continuation is invoked that was captured (using
call-with-current-continuation) during the dynamic extent.It is exited when the called procedure returns.
It is also exited when execution is within the dynamic extent and a continuation is invoked that was captured while not within the dynamic extent.
If a second call to
dynamic-windoccurs within the dynamic extent of the call tothunkand then a continuation is invoked in such a way that theafters from these two invocations ofdynamic-windare both to be called, then theafterassociated with the second (inner) call todynamic-windis called first.If a second call to
dynamic-windoccurs within the dynamic extent of the call tothunkand then a continuation is invoked in such a way that thebefores from these two invocations ofdynamic-windare both to be called, then thebeforeassociated with the first (outer) call todynamic-windis called first.If invoking a continuation requires calling the
beforefrom one call todynamic-windand theafterfrom another, then theafteris called first.The effect of using a captured continuation to enter or exit the dynamic extent of a call to
beforeorafteris unspecified.(let ((path '()) (c #f)) (let ((add (lambda (s) (set! path (cons s path))))) (dynamic-wind (lambda () (add 'connect)) (lambda () (add (call-with-current-continuation (lambda (c0) (set! c c0) 'talk1)))) (lambda () (add 'disconnect))) (if (< (length path) 4) (c 'talk2) (reverse path)))) ===> (connect talk1 disconnect connect talk2 disconnect)
The text herein was sourced and adapted as described in the "R7RS attribution of various text snippets" appendix.