1. Advocacy for a change for the continue key word in while entry loops.

Anyone with experience with while-entry loops either has opted to use goto when you would have wanted to use continue or just has somehow avoided the coding pattern of re-iterating loops. I advocate that the continue key word ought to jump the execution to the entry clause of the while statement rather than the check condition part. The entry clause is where the next line of the data is read.

include std/io.e 
object line 
while atom(line) do 
  if equal(line,"") then 
     -- I want to skip the rest of the code and go to the next line here. 
     -- no such thing in Euphoria. 
  end if 
entry 
  line = gets(STDIN) 
end while 

The jumping to the entry clause is natural because if you convert a for loop into a while entry loop, continue as it is now does the wrong thing.

-- The following code finds the primes smaller than 10: 
sequence primes = {0, 2, 3, 4, 5, 6, 7, 8, 9} 
for i = 1 to 9 do 
  if primes[i] = 0 then 
      continue 
  end if 
  for j = 2*primes[i] to 9 by primes[i] do 
      primes[j] = 0 
  end for 
end for 
? primes 

In converting the above into a while-entry loop, one must change out continue for label/goto or you will get an infinite loop bug

sequence primes = {0, 2, 3, 4, 5, 6, 7, 8, 9} 
integer i = 1 
while i <= 9 with entry do 
  if primes[i] = 0 then 
      -- bug 
      continue 
  end if 
  for j = 2*primes[i] to 9 by primes[i] do 
      primes[j] = 0 
  end for 
entry 
  i += 1 
end while 
? primes 
 
? primes 

While-entry loops do not have any use of continue, and if they do they should be renamed to redo because you are not going to the next iteration but simply doing the same iteration over again. In order to correctly convert the first code to use a while - entry loop you must use goto/label like this:

-- The conversion to a while loop one must get rid of 
-- continue and use goto.  This will infinite loop: 
sequence primes = {0, 2, 3, 4, 5, 6, 7, 8, 9} 
integer i = 1 
while i <= 9 with entry do 
  if primes[i] = 0 then 
      goto "Increment" 
  end if 
  for j = 2*primes[i] to 9 by primes[i] do 
      primes[j] = 0 
  end for 
entry 
label "Increment" 
  i += 1 
end while 
? primes 
 

What do you think?

new topic     » topic index » view message » categorize

2. Re: Advocacy for a change for the continue key word in while entry loops.

SDPringle said...

Anyone with experience with while-entry loops either has opted to use goto when you would have wanted to use continue or just has somehow avoided the coding pattern of re-iterating loops. I advocate that the continue key word ought to jump the execution to the entry clause of the while statement rather than the check condition part. The entry clause is where the next line of the data is read.

(snip)

What do you think?

I'm inclined to agree however I'm unsure if this is the correct solution. I think the entry feature is great and I use it occasionally, but I also think consistency is important and we should remain predictable compared to other languages.

I've perused each of these below and they're all consistent with each other: continue returns to the top of the loop. However, none of them have an "entry clause" like we do, so we may be in uncharted territory here.

C: https://en.cppreference.com/w/c/language/while
C#: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#1392-the-while-statement
C++: https://en.cppreference.com/w/cpp/language/while
Go: https://go.dev/ref/spec#For_condition
Java: https://docs.oracle.com/javase/specs/jls/se23/html/jls-14.html#jls-14.12
JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
Lua: https://www.lua.org/manual/5.4/manual.html#3.3.4
Pascal: https://docwiki.embarcadero.com/RADStudio/Athens/en/Declarations_and_Statements_(Delphi)#Control_Loops
PHP: https://www.php.net/manual/en/control-structures.while.php
Python: https://docs.python.org/3/reference/compound_stmts.html#the-while-statement
Ruby: https://docs.ruby-lang.org/en/3.4/syntax/control_expressions_rdoc.html#label-while+Loop
Rust: https://doc.rust-lang.org/reference/expressions/loop-expr.html
Swift: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow#While-Loops
Visual Basic: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/while-end-while-statement

Technically you can write entry and label on the same line, so this code is valid and IMHO a relatively clean way to solve the problem you're describing.

include std/io.e 
object line 
while sequence(line) with entry do 
    if length(line) = 0 then 
        goto "entry" 
    end if 
entry label "entry" 
    line = gets(STDIN) 
end while 

I'm leaning toward allowing the syntax goto entry as a solution. This would keep continue unchanged, so its behavior remains consistent to when the entry clause is not in play. It's basically shorthand for the example above, and I think the phrase "goto entry" literally speaks for itself.

include std/io.e 
object line 
while sequence(line) with entry do 
    if length(line) = 0 then 
        goto entry 
    end if 
entry 
    line = gets(STDIN) 
end while 

While we're on the topic, another thing that's been musing in the back of my mind lately is allowing the while condition to see within the scope of its loop. If a for loop scopes its variable local only to the loop, why can't a while loop do the same? I'm inclined to require that such variables must be declared at the top of the loop to remain consistent with variable scoping elsewhere.

So this would be legal:

include std/io.e 
while sequence(line) with entry do 
    object line 
    if length(line) = 0 then 
        goto entry 
    end if 
entry 
    line = gets(STDIN) 
end while 

But this would not:

include std/io.e 
while sequence(line) with entry do 
    if length(line) = 0 then 
        goto entry 
    end if 
entry 
    object line = gets(STDIN) 
end while 

I'm entirely open to input on all of this.

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: Advocacy for a change for the continue key word in while entry loops.

ghaberek said...

Technically you can write entry and label on the same line, so this code is valid and IMHO a relatively clean way to solve the problem you're describing.

The entry clause is code that would have gone into the iterator clause of a for loop in a language like python or C++ via OOP or complicated statements in C as in (j && (j=j->next)).

It seems to me the user base is uninterested in this proposed change and the next iteration is rather rare. I think that a change that would attract more uses would get some positive feedback in the forum. So we should probably move on to other ideas should they come.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu