No, loop-and-a-half! (Re: REPEAT... UNTIL ?)

Thomas Bellman bellman at lysator.liu.se
Fri Jul 5 16:47:17 EDT 2002


Greg Ewing <see_reply_address at something.invalid> wrote:

> On the other hand, a situation that *is* very common is
> a loop-and-a-half, with the exit condition in the middle.
> So far, I've never seen *any* really good loop-and-a-half
> structure in any language, and I think Python has a chance
> to be truly innovative here.

As others have already said, iterators can sometimes alleviate
this problem.

And there is *one* language where I think the structure for
loop-and-a-half *is* good: Forth.  The syntax goes something
like

    BEGIN
	spam
	spam
	spam
	eggs?
    WHILE
	norwegian
	blue
    REPEAT

The call to 'eggs?' is the predicate for checking whether the
loop should continue or not; it should put zero (0) on the stack
if it should break, and non-zero if it should continue.  'WHILE'
pops the top number off the stack, and if that number was zero,
it jumps to after 'REPEAT', but if it is non-zero, the loop
continues with the words after 'WHILE'.  The 'REPEAT' word
unconditionally jumps back to 'BEGIN'.

Some dialects of Forth allow several calls to WHILE within the
loop, so it could look like

    BEGIN
	part-1
	test-1
    WHILE
	part-2
	test-2
    WHILE
	part-3
	test-3
    WHILE
	part-4
    REPEAT

I *would* have liked that to be

    repeat:
	part_1()
    while test_1():
	part_2()
    while test_2():
	part_3()
    while test_3():
	part_4()

in Python, but that is unfortunately not compatible with the
current Python syntax. :-(


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"You cannot achieve the impossible without   !  bellman @ lysator.liu.se
 attempting the absurd."                     !  Make Love -- Nicht Wahr!



More information about the Python-list mailing list