[racket-dev] continuation weirdness

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Aug 12 12:24:16 EDT 2010

On Aug 11, 2010, at 3:58 PM, Danny Yoo wrote:

> ;;;;;;;;;;;;;;;;;;;;;;;;;
>> (g1)
> #<continuation>
> #<continuation>
> ;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> where the print-values display of the continuation function appears to
> be printing twice.  I guess I'm trying to understand what DrScheme is
> doing, well enough that I can duplicate this behavior in WeScheme, but
> I'm a bit baffled.


Do you mean to say 

 ".. as opposed to the Racket repl or the DrRacket repl when (g1) is entered individually?"

Posing the question like this suggests that the difference is in the load process, which has an invisible continuation at the end of the three (g1)'s. But I could be way off here. 

;; --- 

I'd write your code like this: 

#lang racket
(define (make-gen gen)
  (define resume-point (box #f))
  (define (result-of-make-gen)
    (let/cc caller-of-result-of-make-gen
      (define (return v)
        (let/cc caller-of-return
          (set-box! resume-point caller-of-return)
          (caller-of-result-of-make-gen v)))
      ;; -- IN --         
      (cond
        [(unbox resume-point) => (lambda (f) (f caller-of-result-of-make-gen))]
        [else (gen return)])))
  ;; -- IN -- 
  result-of-make-gen)

(define g1 (make-gen (lambda (return)
                       (return "a")
                       (return "b")
                       (return "c"))))

(g1)
(g1)
(g1)

Posted on the dev mailing list.