is any other thread ;; waiting to be run, it causes the next thread to run if there ;; is any left to run, otherwise it calls the original exit ;; which exits the whole environment. (define exit ;; The original exit which we override. (let ((exit exit)) ;; The overriding function. (lambda () (if (not (null? ready-list)) ;; There is another thread waiting to be run. ;; So we run it. (let ((cont (car ready-list))) (set! ready-list (cdr ready-list)) ;; Since the ready-list is only non-returning ;; functions, this will not return. (cont #f)) ;; Nothing left to run. ;; The original (exit) is a non returning function, ;; so this is a non-returning function. (exit))))) ;; Takes a one argument function with a given ;; argument and forks it off. The forked function's new ;; thread will exit if/when the function ever exits. (define (fork fn arg) (set! ready-list (append ready-list ;; This function added to the ;; ready-list is non-returning, ;; since exit is non returning. (list (lambda (x) (fn arg) (exit)))))) ;; Gives up control for the next thread waiting to be run. ;; Although it will eventually return, it gives up control ;; and will only regain it when the continuation is called. (define (yield) (call-with-current-continuation ;; Capture the continuation representing THIS call to yield (lambda (cont) ;; Stick it on the ready list (set! ready-list (append ready-list (list cont))) ;; Get the next thread, and start it running. (let ((cont (car ready-list))) (set! ready-list (cdr ready-list)) ;; Run it. (cont #f))))) In 1999, David Madore (inventor of the Unlambda programming language) accidentally discovered a 12-character Unlambda term, making use of call/cc, that printed all natural numbers sequentially in unary representation: ``r`ci`.*`ci. This program and the apparent mystery surrounding its effect have attracted some attention, and are commonly known as the yin-yang puzzle. A Scheme translation, provided by Madore, is as follows: (let* ((yin ((lambda (cc) (display #\@) cc) (call-with-current-continuation (lambda (c) c)))) (yang ((lambda (cc) (display #\*) cc) (call-with-current-continuation (lambda (c) c))))) (yin yang)) Criticism Oleg Kiselyov, author of a delimited continuation implementation for OCaml, and designer of an application programming interface (API) for delimited stack manipulation to implement control operators, advocates the use of delimited continuations instead of the full-stack continuations that call/cc manipulates: "Offering call/cc as a core control feature in terms of which all other control facilities should be implemented turns