Next: , Previous: Error Reporting, Up: Control Mechanisms


26.9 Dynamic Wind

[FIXME: this is pasted in from Tom Lord's original guile.texi and should be reviewed]

— Scheme Procedure: dynamic-wind in-guard thunk out-guard
— C Function: scm_dynamic_wind (in-guard, thunk, out-guard)

Call in-guard, then thunk, then out-guard. All three arguments must be 0-argument procedures.

If, any time during the execution of thunk, the continuation of the dynamic-wind expression is escaped non-locally, out-guard is called. If the continuation of the dynamic-wind is re-entered, in-guard is called. Thus in-guard and out-guard may be called any number of times.

          (define x 'normal-binding)
          ⇒ x
          
          (define a-cont  (call-with-current-continuation
                       (lambda (escape)
                          (let ((old-x x))
                            (dynamic-wind
                               ;; in-guard:
                               ;;
                               (lambda () (set! x 'special-binding))
          
                               ;; thunk
                               ;;
                               (lambda () (display x) (newline)
                                          (call-with-current-continuation escape)
                                          (display x) (newline)
                                          x)
          
                               ;; out-guard:
                               ;;
                               (lambda () (set! x old-x)))))))
          
          ;; Prints:
          special-binding
          ;; Evaluates to:
          ⇒ a-cont
          
          x
          ⇒ normal-binding
          
          (a-cont #f)
          ;; Prints:
          special-binding
          ;; Evaluates to:
          ⇒ a-cont  ;; the value of the (define a-cont...)
          
          x
          ⇒ normal-binding
          
          a-cont
          ⇒ special-binding