A single, general looping construct? (was: why no "do : until"?)
William Sonna
wlsonna at attglobal.net
Wed Jan 3 21:39:56 EST 2001
On Mon, 1 Jan 2001 16:42:42, rturpin at my-deja.com wrote:
> In article <x8G36.156$9s1.31209 at e420r-atl2.usenetserver.com>,
> kragen at dnaco.net (Kragen Sitaker) wrote:
> > I would want to outdent it because, in my view, it isn't
> > part of the loop body; it's part of the loop structure
> > itself, just like the while 1: at the beginning. ..
>
> Hi, Kragen. I see your reasoning, and it reminds me
> of something.
>
> A couple of decades ago, when the programming journals
> were full of arguments about the "right" control
> statements for structured programming, someone proposed
> a single iteration statement that generalized (a) the
> "while" statement, (b) the "do .. until" statement,
> and (c) the loop with an exit in the middle. I forget
> where the paper appeared, the name of the author, and
> the exact syntax. But the core idea, in Pythonic syntax,
> would look like this:
>
> do: <A> while <x>: <B>
>
> Where <A>, and <B> are code blocks, and <x> is the
> conditional. With indentation there are three forms:
>
> # "while" form
> do while <x>: # In this case, make "do" optional
> <B>
> ..
>
> # "repeat .. until" form
> do:
> <A>
> while <x>
> ..
>
> # "exit in the middle" form
> do:
> <A>
> while <x>:
> <B> # Loop continues at "do"
> ..
>
> I always thought this was a slick solution to the question
> of iterative control structures. If the language allows
> repetition of the "while <x>: <B>" in the same loop, this
> construct provides multiple exits. (I mention this, even
> though my first reaction to it is: yuch.) Obviously, the
> language could choose to express the exit condition rather
> than the loop condition, with a keyword such as "until".
>
Interesting concept! Thanks for the info.
Object Rexx's implementation of the generalized do is the best I've
seen (in a real language, that is). Works like this:
do count = 1 to 100 /* fixed number of iterations */
yadda
yadda
end
do while count < 100 /* zero or more iterations */
yadda
yadda
end
do until count >=100 /* one or more iterations */
yadda
yadda
end
do i over extent /* iterate over a collection */
yadda
yadda
end
do forever /* you guessed it */
yadda
yadda
end
Since Python substitutes colon plus indentation for "do" and "end", in
Pythonese, this would look like:
count 1 to 99: # fixed number of iterations
yadda
yadda
while count < 100: # zero or more iterations
yadda
yadda
until count <=100: # one or more iterations
yadda
yadda
count in extent: # iterate over collection
yadda
yadda
forever : # infinite loop
yadda
yadda
Which are all (with the exception of the ones that are identical or
nearly identical) cleaner and even more Pythonic than Python.
How many new keywords? one (if you can part with the unnecessary
"for") or two (if you can't)
> My own view is this: I am plenty happy with "while". If
> Guido decides to expand the iteration constructs, I would
> prefer to see him think about a generalization of the
> "while" statement, rather than adding a second and third.
> Keep it simple. Less is better than more. One way to do
> everything. Etc.
>
Fortran had even simpler control structures than Python, and was a
royal pain to use as a result.
Although I enjoy using Python and am generally supportive of its
minimalist philosophy, I still believe that the "while 1: buried
break" is a step backwards.
Many of the classic algorithms are best expressed with "do until" or
"repeat until" and translating them into Python requires the
introduction of a uniquely inelegant language idiom.
More information about the Python-list
mailing list