Why not allow empty code blocks?

BartC bc at freeuk.com
Sat Jul 30 14:46:33 EDT 2016


On 30/07/2016 16:48, Steven D'Aprano wrote:
> On Sat, 30 Jul 2016 11:58 pm, BartC wrote:
>
>> The 'i' is superfluous. Why not:
>>
>>   for 10:
>
> Why bother? What's so special about this that it needs dedicated syntax?

No named loop variable to invent, create, maintain, and destroy. No 
range object to create, destroy etc. If you're looking for ways for a 
language to be more efficient, then why disregard this possibility?

> Hypertalk (and related XTalk languages) offer a number of dedicated looping
> constructs. Using square brackets [] for optional terms:
>
> repeat [forever]
> repeat [for] number [times]
> repeat until condition
> repeat while condition
> repeat with variable = start [to] end
> repeat with variable = start [down to] end
>
>
> That's right, a bare "repeat" on its own gives you an infinite loop. Does
> *your* language have special syntax for infinite loops? If not, why not?

The infinite loop is:  do ... od

However, there aren't multiple statements here, but just one. The full 
syntax is:

    for i:=A to B by C when D do ... [else...] od

but some parts are optional. So for example:

    for i to B do ... od      # loop from i=1 to B
          to N do ... od      # repeat N times
               do ... od      # repeat forever
    for i:=A   do ... od      # loop from i=A forever

My syntax was based on Algol68 where there is only one loop form and 
it's something like this IIRC:

    for i:=A to B by C while D do ... od

By leaving out the first parts, you end up with a while-loop! So this 
gives you all the possibilities (except repeat ... until/while which is 
missing from the language.).

(In Algol68, a false value in 'while D' will terminate the loop. The 
'when D' condition in my version will only skip that iteration.

But I have a dedicated 'while' loop as well. As has Python. Note that 
the for-loops above iterate only over integers. I use 'forall' for what 
Python calls a for-loop.)

> We should accept that some things are just a matter of taste and idiom. When
> I wrote code in Hypertalk, I used those six different forms and found them
> perfectly reasonable *in that language*. When I write Python code, I never
> find myself wishing I could write Hypertalk code repeat loops (not since
> 1998 or thereabouts) in Python, since they don't "feel" right for the
> language: at best, it would be like suddenly dropping into Lolcat in the
> middle of an ordinary English sentence. Good for a giggle, but that's all.

Well, they wouldn't work for a start. But I'm sure you have needed, at 
some time or other, infinite loops or repeat N times loops. And then you 
just emulate them as best you can with 'while 1:' or 'for _ in 
range(N):' or whatever.

But dedicated forms (even if they just map to 'while' or 'for') wouldn't 
hurt. Syntax is free after all, and it's about expressing exactly what 
you mean.


-- 
Bartc




More information about the Python-list mailing list