[racket-dev] for loops with interleaved escape continuations

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Fri Jun 27 22:40:06 EDT 2014

There are a couple issues here,
One is that #:when in for-clauses allows you to skip iterations, but you want it in the body. This is a cosmetic difference that you can get around by doing #:when (begin imperative-stuff-before-guard guard). If you want ecs to escape an entire for loop, that's what #:break is for. If you want ecs to escape even more for loops, well, that is an unplanned but a possibly useful feature for nested for loops.
The other is that this is not Rackety and thus not natively supported for the same reason there are no "return" statements. The for macros are so much more than Python's for - they can produce values. Don't throw everything into the void :)
---- Original Message -----
From: Jay Kominek <kominek at gmail.com>
To: dev <dev at racket-lang.org>
Sent: Fri, 27 Jun 2014 17:15:45 -0400 (EDT)
Subject: [racket-dev] for loops with interleaved escape continuations

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


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
  Racket Developers list:

Posted on the dev mailing list.