Name: Anonymous 2013-03-02 2:18
In this thread, you may post code that makes use of continuations, and I will attempt to produce code that does the same and looks reasonably similar, without using continuations.
make-list-of-n-resources
make-io-port
start-io-event
...
;; A thread is a stack of functions, waiting to be called with a return value.
;; Tail calls are used to pass control between threads. The native function call stack
;; remains one layer deep.
(define (thread-return thread return-value)
(if (null? thread)
(error "nothing to return to!")
((car thread) (cdr thread) return-value)))
(define (thread-call thread return-handler callee . callee-args)
(apply callee (cons (cons return-handler thread) callee-args)))
(define (thread-tail-call thread callee . callee-args)
(apply callee (cons thread callee-args)))
(define (fact thread n acc)
(if (= n 0)
(thread-return thread acc)
(thread-tail-call thread fact (- n 1) (* n acc))))
(define (fact2 thread n)
(if (= n 0)
(thread-return thread 1)
(thread-call thread
(lambda (thread ret-val)
(thread-return thread (* n ret-val)))
fact2 (- n 1))))
(define (print t v)
(display v)
(newline))
(fact (list print) 20 1)
(fact2 (list print) 20)