Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

continuations

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.

Name: Anonymous 2013-03-02 14:43

>>31

I would translate that, but there are too many undefined symbols...


make-list-of-n-resources
make-io-port
start-io-event
...


I would fill in the gaps with my own implementations but I could be oversimplifying the problem. So instead, here's the start of a thread manager...


;; 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)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List