do...until wisdom found...

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Wed Apr 18 16:47:45 EDT 2001


Tue, 17 Apr 2001 09:28:15 -0700, Bob Cannard <bob_cannard at mentor.com> pisze:

> As a result, most non-for loops degenerate into
> 
>         while 1:
>             set up for this cycle
>             if c: break
>             whatever needs to be done

Some languages express loops with no looping keywords at all.
Loops of this style are written thus:

    def loop():
        set up for this cycle
        if not c:
            whatever needs to be done
            loop()
    loop()

(and run in constant memory despite the recursion). It's simpler in
the idea than those while/do/until/break/repeat stories because it
doesn't need more additional concepts than those which have to be
present anyway.

When a state is to be maintained through the loop, it's usually passed
as arguments to the recursive function.

Since it's sometimes too verbose, there are functions which wrap common
patterns of loops. This is where map, filter and reduce came from, and
list comprehensions.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list