1. Labeled Loops?
CChris wrote:
>
> }}}
<eucode>
> label "my_loop"
> for i=1 to n do
> while 1 do
> while f(x)=3 do
> ---....
> if done() then
> exit "my_loop"
> end if
> end end end
> </eucode>
{{{
>
Hm... what about:
for a = 1 to 10 label "top" do
for b = 1 to 10 label "mid" do
while 1 do
if b = 3 then exit "top" end if
if a = 2 then continue "mid" end if
end while
end for
end for
A terrible loop example, but the ability to label loops. The addition of this
labeling and exit/continue to those labels is cake.
This is just me thinking aloud again once seeing Chris's example code. I kind of
really like this idea. What about you? label is obviously optional, just as "by"
is on a for loop. The implementation of such would allow you to reuse labels in
non related loops and again, would cause no slow down what-so-ever if used or not
used.
--
Jeremy Cowgar
http://jeremy.cowgar.com
2. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> for a = 1 to 10 label "top" do
> for b = 1 to 10 label "mid" do
> while 1 do
> if b = 3 then exit "top" end if
> if a = 2 then continue "mid" end if
> end while
> end for
> end for
or maybe:
for a = 1 to 10 do
for b = 1 to 10 do
while 1 do
if b = 3 then exit a end if
if a = 2 then continue b end if
end while
end for
end for
3. Re: Labeled Loops?
c.k.lester wrote:
>
> or maybe:
>
> for a = 1 to 10 do
> for b = 1 to 10 do
> while 1 do
> if b = 3 then exit a end if
> if a = 2 then continue b end if
> end while
> end for
> end for
Ok, genius, how do you label a while loop? :P
Matt
4. Re: Labeled Loops?
Matt Lewis wrote:
> c.k.lester wrote:
> > or maybe:
> > for a = 1 to 10 do
> > for b = 1 to 10 do
> > while 1 do
> > if b = 3 then exit a end if
> > if a = 2 then continue b end if
> > end while
> > end for
> > end for
>
> Ok, genius, how do you label a while loop? :P
while 1 label "matt_is_a_poopie_head" do
...
exit "matt_is_a_poopie_head"
end while
Of course, an undefined exit takes you out of the latest loop.
while 1 do
exit
end while
or something like this:
while 1 do "getrdone"
while 1 do "inner_while"
if y then
exit "getrdone"
else
exit "getrdone"
end if
end while
but I still say for loops need no label. :)
5. Re: Labeled Loops?
Matt Lewis wrote:
>
> c.k.lester wrote:
> >
> > or maybe:
> >
> > for a = 1 to 10 do
> > for b = 1 to 10 do
> > while 1 do
> > if b = 3 then exit a end if
> > if a = 2 then continue b end if
> > end while
> > end for
> > end for
>
> Ok, genius, how do you label a while loop? :P
>
Same way. Cake! I already have the code in my head floating around. I didn't add
one just to show you didn't have to.
while TRUE label "top" do
for a = 1 to 10 label "mid" do
for b = 1 to 10 label "bottom" do
exit "top"
end for
end for
end while
If this is worth while, I'll code it up tonight (possibly). I am having too much
fun with conditional compiling. Wait for my next post. I think everyone will be
excited about the possibility.
--
Jeremy Cowgar
http://jeremy.cowgar.com
6. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> >
> > Ok, genius, how do you label a while loop? :P
> >
>
> Same way. Cake! I already have the code in my head floating around. I didn't
> add one just to show you didn't have to.
>
> }}}
<eucode>
> while TRUE label "top" do
> for a = 1 to 10 label "mid" do
> for b = 1 to 10 label "bottom" do
> exit "top"
> end for
> end for
> end while
> </eucode>
{{{
>
> If this is worth while, I'll code it up tonight (possibly). I am having too
> much fun with conditional compiling. Wait for my next post. I think everyone
> will be excited about the possibility.
>
Opps! It wasn't until I read CK's response that I realized it was directed to
him and it was his code! So much for reading
Sorry CK!
--
Jeremy Cowgar
http://jeremy.cowgar.com
7. Re: Labeled Loops?
c.k.lester wrote:
>
> while 1 do "getrdone"
> while 1 do "inner_while"
> if y then
> exit "getrdone"
> else
> exit "getrdone"
> end if
> end while
>
> but I still say for loops need no label. :)
I think for consistency, for and while loops should be labeled the same.
--
Jeremy Cowgar
http://jeremy.cowgar.com
8. Re: Labeled Loops?
Matt Lewis wrote:
>
> c.k.lester wrote:
> >
> > or maybe:
> >
> > for a = 1 to 10 do
> > for b = 1 to 10 do
> > while 1 do
> > if b = 3 then exit a end if
> > if a = 2 then continue b end if
> > end while
> > end for
> > end for
>
> Ok, genius, how do you label a while loop? :P
>
> Matt
Yeah, using the loop var as the exit or continue target for a "for" statement
makes sense to me. But as you and Jeremy pointed out, it /wouldn't/ make sense in
the context of "while" loops.
Hmm.
--
A complex system that works is invariably found to have evolved from a simple
system that works.
--John Gall's 15th law of Systemantics.
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.
9. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> c.k.lester wrote:
> >
> > while 1 do "getrdone"
> > while 1 do "inner_while"
> > if y then
> > exit "getrdone"
> > else
> > exit "getrdone"
> > end if
> > end while
> >
> > but I still say for loops need no label. :)
>
> I think for consistency, for and while loops should be labeled the same.
I agree that they should be treated the same. I just like giving CK a
hard time.
Matt "poopie head" Lewis
10. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> c.k.lester wrote:
> >
> > while 1 do "getrdone"
> > while 1 do "inner_while"
> > if y then
> > exit "getrdone"
> > else
> > exit "getrdone"
> > end if
> > end while
> >
> > but I still say for loops need no label. :)
>
> I think for consistency, for and while loops should be labeled the same.
They're not consistent now... so what do you mean?
11. Re: Labeled Loops?
c.k.lester wrote:
>
> > I think for consistency, for and while loops should be labeled the same.
>
> They're not consistent now... so what do you mean?
The means of labeling and addressing them:
while 1 label "top" do
for a = 1 to 10 label "mid" do
end for
end while
I do not think you should refer to the for loop as "a", I think when you want a
label, you give it one for consistencies sake.
--
Jeremy Cowgar
http://jeremy.cowgar.com
12. Re: Labeled Loops?
Jeremy Cowgar wrote:
> c.k.lester wrote:
> > > I think for consistency, for and while loops should be labeled the same.
> > They're not consistent now... so what do you mean?
> The means of labeling and addressing them:
> while 1 label "top" do
> for a = 1 to 10 label "mid" do
> end for
> end while
>
> I do not think you should refer to the for loop as "a", I think when you want
> a label, you give it one for consistencies sake.
Can't it be simpler?
while 1 do "top"
for a = 1 to 10 do "mid"
end for
end while
or, if you absolutely need a "label" statement
while 1 do ~top
for a = 1 to 10 do ~mid
end for
end while
13. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> c.k.lester wrote:
> >
> > > I think for consistency, for and while loops should be labeled the same.
> >
> > They're not consistent now... so what do you mean?
>
> The means of labeling and addressing them:
>
> }}}
<eucode>
> while 1 label "top" do
> for a = 1 to 10 label "mid" do
> end for
> end while
> </eucode>
{{{
>
> I do not think you should refer to the for loop as "a", I think when you want
> a label, you give it one for consistencies sake.
>
> --
> Jeremy Cowgar
> <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a>
Just to be clear, for labeled loops, I would hope that the programmer would
choose a better variable name than "a".
--
A complex system that works is invariably found to have evolved from a simple
system that works.
--John Gall's 15th law of Systemantics.
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.
14. Re: Labeled Loops?
c.k.lester wrote:
>
> > while 1 label "top" do
> > for a = 1 to 10 label "mid" do
> > end for
> > end while
> >
> > I do not think you should refer to the for loop as "a", I think when you
> > want
> > a label, you give it one for consistencies sake.
>
> Can't it be simpler?
>
> while 1 do "top"
> for a = 1 to 10 do "mid"
> end for
> end while
>
> or, if you absolutely need a "label" statement
>
> while 1 do ~top
> for a = 1 to 10 do ~mid
> end for
> end while
I think for as much as it will be used and for clarity and parsing reasons,
label "abc" would be best, but others may disagree. It especially will not work
after the do. It would be hard to detect if it's the first execution token of the
block or a label. Also, I do not think the label would need to be quoted, it
would just be label top.
--
Jeremy Cowgar
http://jeremy.cowgar.com
15. Re: Labeled Loops?
Jason Gade wrote:
>
> Just to be clear, for labeled loops, I would hope that the programmer would
> choose a better variable name than "a".
>
Depends on how complex the loop is for me. If it's only a few lines, I'll
commonly use a,b,c or i,j,k for loop variables.
for a in 1 to length(lines) do
line = lines[a]
puts(output, trim(" \t\r\n", line) & "\n")
end for
You'll find code like that in my production apps. Now, am I going to do a lot of
processing on it? Is the loop many lines long? Sure, a more appropriate name will
be given, but in the above example? nah, a is just fine for me.
--
Jeremy Cowgar
http://jeremy.cowgar.com
16. Re: Labeled Loops?
I'm not sure I'm following here.
But say it's implemented so the syntax is:
while 1 do label top
for i = 1 to 10 do label mid
? i
end for
end while
Where top and mid have a scope similar to "for" variables -- that is, to the end
of their respective "end" statements and couldn't be used otherwise as variables.
But they wouldn't be objects or anything. Hmm.
Maybe that's a reason to make them string or sequence constants instead?
Still kinda confused.
--
A complex system that works is invariably found to have evolved from a simple
system that works.
--John Gall's 15th law of Systemantics.
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.
17. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> Jason Gade wrote:
> >
> > Just to be clear, for labeled loops, I would hope that the programmer would
> > choose a better variable name than "a".
> >
>
> Depends on how complex the loop is for me. If it's only a few lines, I'll
> commonly
> use a,b,c or i,j,k for loop variables.
>
> }}}
<eucode>
> for a in 1 to length(lines) do
> line = lines[a]
> puts(output, trim(" \t\r\n", line) & "\n")
> end for
> </eucode>
{{{
>
> You'll find code like that in my production apps. Now, am I going to do a lot
> of processing on it? Is the loop many lines long? Sure, a more appropriate
> name
> will be given, but in the above example? nah, a is just fine for me.
>
> --
> Jeremy Cowgar
> <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a>
Well, yeah, I do loops like that too for simple loops. But for something where
you are going to have some kind of complicated exit/continue semantics seems like
a not simple loop.
--
A complex system that works is invariably found to have evolved from a simple
system that works.
--John Gall's 15th law of Systemantics.
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.
18. Re: Labeled Loops?
Jason Gade wrote:
>
> I'm not sure I'm following here.
>
> But say it's implemented so the syntax is:
> }}}
<eucode>
> while 1 do label top
> for i = 1 to 10 do label mid
> ? i
> end for
> end while
> </eucode>
{{{
>
It would be slightly different
while 1 label top do
for i = 1 to 10 label mid do
? i
end for
end while
> Where top and mid have a scope similar to "for" variables -- that is, to the
> end of their respective "end" statements and couldn't be used otherwise as
> variables.
> But they wouldn't be objects or anything. Hmm.
>
> Maybe that's a reason to make them string or sequence constants instead?
>
> Still kinda confused.
>
Internally a for loop and while loop have a stack where a reference to the loop
variable is stored (for loop), the position in IL where the loop starts and ends.
A label would simply be added to that stack item associated with the loop. Thus,
the label could be considered an internal loop identifier. It has to be neither a
sequence or integer. It simply has to exist, if you give the loop a "label"
keyword.
--
Jeremy Cowgar
http://jeremy.cowgar.com
19. Re: Labeled Loops?
Jason Gade wrote:
>
> Well, yeah, I do loops like that too for simple loops. But for something where
> you are going to have some kind of complicated exit/continue semantics seems
> like a not simple loop.
It was merely an example
--
Jeremy Cowgar
http://jeremy.cowgar.com
20. Re: Labeled Loops?
> On 12 May 2008 at 17:01, Jeremy Cowgar wrote (maybe snipped):
> Hm... what about:
>
> }}}
<eucode>
> for a = 1 to 10 label "top" do
> for b = 1 to 10 label "mid" do
> while 1 do
> if b = 3 then exit "top" end if
> if a = 2 then continue "mid" end if
> end while
> end for
> end for
> </eucode>
{{{
>
I agree if all we need is "breaking" loop laces. But if I need to go
to "Far Far Away" then probably we'd need "goto". The problem I see
is that in an Euphorian way, labels should (must?) be declared prior
to use, just like variables, so this would be a problem in most
cases.
Though I never need it before (since those times of BASIC and COBOL),
there are times I need to re-think a large part of the code so I
don't get stuck on lace-structured code. Goto and gosub would get me
out of the trouble in a snap, but my code would look (and would be)
so ugly and (probably) so difficult to maintain that I can't see the
real benefit of it. But this is just me. ;)
And Jeremy, just trying to understand the concept of "exit" and
"continue" on the example above: continue "mid" exits the while loop
and goes to next iteration of "mid" for...end for loop. OTOH, exit
"top" exits the whole thing and code will continue execution after
"top" for...end for loop, if any. Did I get it right? Sorry, but I'm
getting these nouveaut=E9s quite slowly. :)
Best,
Euler
--
_
_| euler f german
_| sete lagoas, mg, brazil
_| efgerman{AT}gmail{DOT}com
21. Re: Labeled Loops?
Euler German wrote:
>
> > On 12 May 2008 at 17:01, Jeremy Cowgar wrote (maybe snipped):
>
> > Hm... what about:
> >
> > }}}
<eucode>
> > for a = 1 to 10 label "top" do
> > for b = 1 to 10 label "mid" do
> > while 1 do
> > if b = 3 then exit "top" end if
> > if a = 2 then continue "mid" end if
> > end while
> > end for
> > end for
> > </eucode>
{{{
> >
>
> I agree if all we need is "breaking" loop laces. But if I need to go
> to "Far Far Away" then probably we'd need "goto". The problem I see
> is that in an Euphorian way, labels should (must?) be declared prior
> to use, just like variables, so this would be a problem in most
> cases.
No, labels would neither be declared nor usable in anything but a exit or
continue. i.e.
while 1 label hello do
? hello
end while
You will get a compile error of an unknown variable hello. Their use in continue
and exit will be special uses and will search the loop stack for the
corresponding loop label, not a variable stack or anything.
>
> And Jeremy, just trying to understand the concept of "exit" and
> "continue" on the example above: continue "mid" exits the while loop
> and goes to next iteration of "mid" for...end for loop. OTOH, exit
> "top" exits the whole thing and code will continue execution after
> "top" for...end for loop, if any. Did I get it right? Sorry, but I'm
> getting these nouveaut=E9s quite slowly. :)
>
In the above example... exit "top" exits to the loop with the label "top".
continue "mid" continues with next iteration of the loop labeled "mid".
We would probably need a special label such as "all" or something that would
exit all loops.
--
Jeremy Cowgar
http://jeremy.cowgar.com
22. Re: Labeled Loops?
Jeremy Cowgar wrote:
>
> CChris wrote:
> >
> > }}}
<eucode>
> > label "my_loop"
> > for i=1 to n do
> > while 1 do
> > while f(x)=3 do
> > ---....
> > if done() then
> > exit "my_loop"
> > end if
> > end end end
> > </eucode>
{{{
> >
>
> Hm... what about:
>
> }}}
<eucode>
> for a = 1 to 10 label "top" do
> for b = 1 to 10 label "mid" do
> while 1 do
> if b = 3 then exit "top" end if
> if a = 2 then continue "mid" end if
> end while
> end for
> end for
> </eucode>
{{{
>
> A terrible loop example, but the ability to label loops. The addition of this
> labeling and exit/continue to those labels is cake.
>
> This is just me thinking aloud again once seeing Chris's example code. I kind
> of really like this idea. What about you? label is obviously optional, just
> as "by" is on a for loop. The implementation of such would allow you to reuse
> labels in non related loops and again, would cause no slow down what-so-ever
> if used or not used.
>
> --
> Jeremy Cowgar
> <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a>
Actually that's how "label" is used in Æ with no gotoes...
CChris
23. Re: Labeled Loops?
> On 12 May 2008 at 20:01, Jeremy Cowgar wrote (maybe snipped):
> Euler German wrote:
> >
> > > On 12 May 2008 at 17:01, Jeremy Cowgar wrote (maybe snipped):
> >
> > > Hm... what about:
> > >
> > > }}}
<eucode>
> > > for a = 1 to 10 label "top" do
> > > for b = 1 to 10 label "mid" do
> > > while 1 do
> > > if b = 3 then exit "top" end if
> > > if a = 2 then continue "mid" end if
> > > end while
> > > end for
> > > end for
> > > </eucode>
{{{
> > >
> >
> > I agree if all we need is "breaking" loop laces. But if I need to go
> > to "Far Far Away" then probably we'd need "goto". The problem I see
> > is that in an Euphorian way, labels should (must?) be declared prior
> > to use, just like variables, so this would be a problem in most
> > cases.
>
> No, labels would neither be declared nor usable in anything but a exit
> or continue. i.e.
>
>
> }}}
<eucode>
> while 1 label hello do
> ? hello
> end while
> </eucode>
{{{
>
> You will get a compile error of an unknown variable hello. Their use
> in continue and exit will be special uses and will search the loop
> stack for the corresponding loop label, not a variable stack or
> anything.
>
Hmmm, I got that. What I was saying "labels" would be part of while
and for laces *only*, that is, there'll be no outsiders that could be
used, say, for a "goto". Let me go a little further and play the
Devil's lawyer.
while 1 label hello do
...
if FALSE exit hello end if
...
end while
while 1 label hi do
...
if FALSE exit hello end if -- note "hello" in place of "hi"
...
end while
I would expect an error as "hello" isn't part of the second loop
stack, right? Maybe I'm exaggerating but that is why I don't like
labels. They can be easily misplaced. If escape conditions are to be
constrained to loops I see very little benefit of using it.
> >
> > And Jeremy, just trying to understand the concept of "exit" and
> > "continue" on the example above: continue "mid" exits the while loop
> > and goes to next iteration of "mid" for...end for loop. OTOH, exit
> > "top" exits the whole thing and code will continue execution after
> > "top" for...end for loop, if any. Did I get it right? Sorry, but I'm
> > getting these nouveaut=E9s quite slowly. :)
> >
>
> In the above example... exit "top" exits to the loop with the label
> "top". continue "mid" continues with next iteration of the loop
> labeled "mid".
>
Do you mean it'll restart "top" loop from 1? I'm a bit confused. I
understand "continue" very clearly. It means (to me) "forget this
one, but continue trying with next". But what I get from "exit" is
just "left this and go ahead".
> We would probably need a special label such as "all" or something that
> would exit all loops.
>
I don't know, really. You guys are light-years ahead of me. But it
looks more as shortcuts to gain nanoseconds or ways to get into a
hole an emerge in a parallel dimension.
Please, don't take this as criticism. I want to contribute with my
lack of knowledge so Euphoria still be attractive to newcomers.
Best,
Euler
--
_
_| euler f german
_| sete lagoas, mg, brazil
_| efgerman{AT}gmail{DOT}com
24. Re: Labeled Loops?
No, labels in this sense would not be used anywhere else, only in loop bodies.
There are many situations where it would be helpful to break cleanly out of
nested loops. The other method is to setup conditions in the parent loops to
watch for a var change in a child loop.
--
Jeremy Cowgar
http://jeremy.cowgar.com