;; dynamic-wind ;; A function that safely processes an input file (define (safe-process input-file process-fn) (let ((p (open-input-file input-file))) (dynamic-wind (lambda () #f) (lambda () (process-fn p)) (lambda () (begin (display "closing input port...") (newline) (close-input-port p)))))) ;; Define a processing function that always exits to toplevel. (define toplevel-exit 'dummy) (call/cc (lambda (cont) (set! toplevel-exit cont))) (define (process p) (toplevel-exit)) ;; Observe that dynamic-wind still executes the code which ;; closes the input port. (safe-process "lecture14-dynamic-wind.ss" process)