4 ms·
Part of the async i/o solution for Guile involves "prompts" (more formally delimited continuations) that allow defining new types of control flow operators. He
by davexunit 10y ago
Part of the async i/o solution for Guile involves "prompts" (more formally delimited continuations) that allow defining new types of control flow operators. Here's a simple cooperative multitasking implementation and a contrived example program that uses it:
;; Core implemenation
(define (call-with-coroutine thunk)
"Apply THUNK with a coroutine prompt."
(define (handler cont callback . args)
(define (resume . args)
;; Call continuation that resumes the procedure.
(call-with-prompt 'coroutine-prompt
(lambda () (apply cont args))
handler))
(when (procedure? callback)
(apply callback resume args)))
;; Call procedure.
(call-with-prompt 'coroutine-prompt thunk handler))
(define (yield callback)
"Yield continuation to a CALLBACK procedure."
(abort-to-prompt 'coroutine-prompt callback))
;; Simplistic example
(define *cont* #f)
(define (pause)
(yield
(lambda (cont)
(set! *cont* cont))))
(define (resume)
(*cont*))
;; Prints "foo"
(call-with-coroutine
(lambda ()
(display "foo\n")
(pause)
(display "bar\n")
(pause)
(display "baz\n")))
(resume) ; prints "bar"
(resume) ; prints "baz"
- nalaginrut 10y agoI've no any doubt people will understand how good it is for various kind of coroutines usage when they come to Guile. What we need is more libs and projects for reducing workload in practices.