while (a=b()) ...

Dan Schmidt dfan at harmonixmusic.com
Fri May 14 11:30:08 EDT 1999


mlh at idt.ntnu.no (Magnus L. Hetland) writes:

| Dan Schmidt <dfan at harmonixmusic.com> writes:
| 
| > Your loop would look something like this:
| >
| >   while 1 until done:
| >     value = getValue()
| >     if not value: done
| >     process(value)
| >
| > 'done' is not a keyword; it's basically an exception (the name of
| > which was generated by the 'until' clause) that kicks us out of the
| > loop.
| 
| This is identical to the "detect" structure developed by Arne Halaas
| in the '70ies (and which has been discussed here at least twice
| already.)

Neat.  Knuth's article is actually earlier than Halaas' (it was
published in 1974), it turns out.

| The detect-version would be:
| 
| detect done:
|     value = getValue()
|     if not value: done
|     proces(value)
| 
| (Although the structure can actually do more than that...)
| 
| For a simple implementation in Python, see
| 
|    http://www.idi.ntnu.no/~mlh/python

Thanks for the reference.  I looked up your informative December post
in dejanews (you might want to put a copy of it on your web site; it's
better than no documentation at all).

One thing which Knuth's construct (actually, he stole it from Zahn)
has that Halaas' lacks is the ability to catch the break-exceptions
from outside the loop, along with information associated with the
exception.  There was a (trivial) example of this in the second half
of my article you replied to.  However, your implementation could be
extended to do that too, since it is using the exception mechanism
underneath.

I've never had the opportunity to use any of these constructs, so I
have no opinion on which is better :)

-- 
                 Dan Schmidt -> dfan at harmonixmusic.com, dfan at alum.mit.edu
Honest Bob & the                http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
          Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/




More information about the Python-list mailing list