[plt-scheme] Another continuation question

From: Grant Rettke (grettke at acm.org)
Date: Sat Jul 19 16:03:52 EDT 2008

In a thread that I can't find, I'm pretty sure that Matthias posted
some interesting "self modifying code" that looked like this, which I
found on:

http://en.wikipedia.org/wiki/Call-with-current-continuation

;; [LISTOF X] -> ( -> X u 'you-fell-off-the-end-off-the-list)
(define (generate-one-element-at-a-time a-list)
  ;; (-> X u 'you-fell-off-the-end-off-the-list)
  ;; this is the actual generator, producing one item from a-list at a time
  (define (generator)
    (call/cc control-state))
  ;; [CONTINUATION X] -> EMPTY
  ;; hand the next item from a-list to "return" (or an end-of-list marker)'
  (define (control-state return)
    (for-each
     (lambda (an-element-from-a-list)
       (call/cc
        (lambda (resume-here)
          (set! control-state resume-here)
          (return an-element-from-a-list))))
     a-list)
    (return 'you-fell-off-the-end-off-the-list))
  ;; time to return the generator
  generator)

(define gen (generate-one-element-at-a-time '(a b c)))
(gen)
(gen)
(gen)
(gen)

This is another chunk of code that worked fine in v372, and per the
thread [Confusing continuation behavior (a question about)], I suspect
that the code shouldn't have worked, because now it goes into an
endless loop?

I'm migrating all of my collected code snips from 3 to 4, perhaps the
solution is the same as the other thread, but I haven't figured that
one out yet ;).


Posted on the users mailing list.