Looping [was Re: Python and the need for speed]

Ben Bacarisse ben.usenet at bsb.me.uk
Sun Apr 16 07:02:51 EDT 2017


Steve D'Aprano <steve+python at pearwood.info> writes:

<snip>
> I don't remember the language, but I remember seeing one generalisation of
> the repeat/do loop that puts the test in the middle, rather than at the
> start or end of the loop. If I remember it was something like:
>
> DO
>     setup code  # executed once only
> REPEAT
>     loop body  # before the test
> WHILE condition  # test
>     loop body  # after the test
>
> thus combining both the 
>
>     repeat while condition:
>         ...
>
> and
>
>     do:
>         ...
>     until condition
>
> styles of looping in one handy syntax.

Was the setup code in the loop because that seems unnecessary (I know
this is from memory)?  I have a recollection of using something like

  do
      code before test
    while cond
      code after test
  od

which has much the same effect.  Algol 68 can also do this because the
while condition can be a block (any expression can be a block in A68).
The result was not to everyone's taste.

> On Sat, 15 Apr 2017 10:17 pm, bartc wrote:
<snip>
>> Of course, it's possible to overdo it; if you look at Lisp, you'll lose
>> yourself in the myriad looping options.
>
> "Myriad"? As I understand it, Lisp offers just *one* more loop construct
> than the number you agree is the "minimum" needed: five.
>
> loop
> loop for
> do
> dotimes
> dolist

I suppose it depends on how you count, but BartC did talk about
options.  Here are some of the loop options in Common Lisp:

  (loop x with ...)
  (loop for i in (...) by ...)
  (loop for i downfrom 10 above x by -2 ...)
  (loop for x = ... then (f x) ...)
  (loop across ...)
  (loop for s being symbols of ...)
  (loop for s being hash-keys in ... using ...)

That's just the start.  Then you can add "doing", "if" "when", "unless",
"return", "collect", "sum[ming]", "maximizing" and others.  Or, if you
need them, you can have "initially", "finally", "repeat", "while",
"until", "always", "never" or "thereis" parts, all the time losing (some
of) those parts you don't need.

<snip>
-- 
Ben.



More information about the Python-list mailing list