[racket-dev] for loops with interleaved escape continuations

From: Jay Kominek (kominek at gmail.com)
Date: Fri Jun 27 17:15:45 EDT 2014

I've been converting a bunch of Python to Racket lately, and I have a
lot of loops that use break and continue. I end up turning them into:

(let/ec break
  (for (...)
    (let/ec continue
      ; do some work
      (when this-iteration-isn't-what-i-want
        (continue))
      ; do more expensive work
      (when found-what-i-want
        (break what-i-want)))))

I thought it would be nice if the let/ec's could be integrated with
for, so that you could instead write:

(for (#:ec break
       ...
       #:ec continue)
      ; ...same as above...

In an attempt to help convey the behavior I want, I threw this patch together:

https://github.com/jkominek/racket/commit/b291a0b994c679445b3210bd3efba8c6cea867e4

I feel it behaves reasonably when using for and for/fold, but for/list
doesn't behave in any way I'd hope for.

Ideally somebody who understands for's implementation will agree that
this is a great idea, and go make it all work nicely. :) Failing that
I'm open to suggestions for how to make it behave better, in a fashion
which would make it appropriate for inclusion.

-- 
Jay Kominek

Posted on the dev mailing list.