;; routine.scm - (c) rohan drape, 2005-2006 ;; `z' is a procedure of two arguments. The first is an initial ;; input, the second a procedure of one argument that is used to ;; 'yield' a value. Yielding returns the value to the initiator of ;; the routine, and pauses the routine at the current location until ;; it is initiated again. (define-structure routine step undo) (define (make-routine* z) (let* ((a #f) (b #f) (goto-a (lambda (result) (call/cc (lambda (exit) (set! b exit) (if (not a) (error "routine: goto-a: a not set?") (a result)))))) (goto-b (lambda (input) (call/cc (lambda (exit) (set! a exit) (if b (b input) (z input goto-a)))))) (undo (lambda () (set! a #f) (set! b #f)))) (make-routine goto-b undo))) (define (routine-next! r value) ((routine-step r) value)) (define (routine-reset! r) ((routine-undo r))) ;; (f yield) where (yield n) pauses execution for interval n. (define (schedule-routine! T u f) (let ((r (make-routine* (lambda (_ yield) (f yield))))) (at T u (lambda (t f*) (f* (routine-next! r t)))))) (define (tschedule-routine! T p f) (let ((r (make-routine* (lambda (_ yield) (f yield))))) (tschedule-at! T p (lambda (p f*) (f* (routine-next! r p))))))