## [ Poll ] Infinite loops

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

warriorness
Huge Fucking-Lazer
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA
Contact:

### [ Poll ] Infinite loops

This is a poll thread without an actual poll, since were I to make a poll I'd surely miss many options and be yelled at.

What is the best way to implement an infinite loop?

I'm a fan of the "for (;;) { ... }" method, but I've seen it done in various other ways, such as "while (true)" and "while(1==1)".
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work

cmacis
Posts: 754
Joined: Wed Dec 13, 2006 5:22 pm UTC
Contact:
The best way is by mistake and entirely accidentally, followed by much head scratching (and resultant hair loss) over why your loop never yields false. Sending i the wrong way is always a good one, and easily overlooked:

Code: Select all

`for(int i=10;i<11;i--)`

If you just occasionally use a backwards loop, but still use less than. Yep, brilliant.

Oh run time errors, how we love ye.
li te'o te'a vei pai pi'i ka'o ve'o su'i pa du li no
Mathematician is a function mapping tea onto theorems. Sadly this function is irreversible.
QED is Latin for small empty box.
Ceci nâ€™est pas une [s]pipe[/s] signature.

demon
Posts: 170
Joined: Tue Feb 06, 2007 8:13 pm UTC
i usually write while(1) { }, but i might change to while(42):]

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Contact:
while(true).

for(;; ) seems more idiomatic, but while(true) seems to make more sense to me.

simen
Posts: 166
Joined: Wed Feb 21, 2007 7:02 pm UTC
Contact:
I like this one, if it counts. Mainly for the hell of it.

Code: Select all

`def loop():   while 1:      try: loop()      except RuntimeError: loop()`

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC
Some people like

Code: Select all

`#define EVER ;;...for(EVER)`

I usually go with while(1) or while(true), though.

SpitValve
Not a mod.
Posts: 5130
Joined: Tue Sep 26, 2006 9:51 am UTC
Location: Lower pork village

Code: Select all

`10 PRINT "LOOK AROUND YOU"20 GOTO 10`

bitwiseshiftleft
Posts: 295
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford
Contact:
Personally, my favorite is the bash forkbomb, ":(){:|:&};:", although summing the integers with "sum [1..]" is pretty amusing too. This, unfortunately, does not give correct the answer of -1/12.

Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Code: Select all

`while(true) {}`

is my usual method.

Code: Select all

`for(int cnt = 0;true; ++cnt) {}`

has some appeal -- you can keep track of how fast your loop is spinning, and the overhead is cheap.

Code: Select all

`def repeat(func, *arglist):  func(arglist)  return repeat(func, *arglist)`

has a certain charm -- it is a neat way to test if your compiler is smart enough to detect and optimize tail-end recursion (and if your language definition allows it to be optimized away)

Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC
while true. Code should be readable.

Yakk: Guido seems to be very anti-functional. He's said that tail-call optimization will never be implemented in Python, and he's going to remove map, filter and reduce in Python 3000 (he wanted to remove lambda too, but he got too much resistance for that).

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Contact:
Andrew wrote:Like this.

Haha... that's great

Stig Hemmer
Posts: 30
Joined: Wed Feb 28, 2007 4:46 pm UTC
Location: Trondheim, Norway
Andrew for the win.

Code: Select all

`((lambda (f) (f f))(lambda (f) (f f)))`

I normally prefer Common Lisp to Scheme, but silly things like this looks better in Scheme.
This is not true.

notzeb
Without Warning
Posts: 629
Joined: Thu Mar 08, 2007 5:44 am UTC
Location: a series of tubes

Code: Select all

`+[]`

Code: Select all

`!/\ \/`
Zµ«V­jÕ«ZµjÖ­Zµ«VµjÕ­ZµkV­ZÕ«VµjÖ­Zµ«V­jÕ«ZµjÖ­ZÕ«VµjÕ­ZµkV­ZÕ«VµjÖ­Zµ«V­jÕ«ZµjÖ­ZÕ«VµjÕ­ZµkV­ZÕ«ZµjÖ­Zµ«V­jÕ«ZµjÖ­ZÕ«VµjÕ­Z

Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove
Then what was learned from Python will be taken and used to build another language that won't suck.

:)

bitwiseshiftleft
Posts: 295
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford
Contact:
Map and filter aren't used much in Python because they're subsumed by list comprehensions, which are much easier to read if you're not a functional programmer

Reduce isn't used much in Python because for loops are more idiomatic.

Dingbats
Posts: 921
Joined: Tue Mar 20, 2007 12:46 pm UTC
Location: Sweden
Contact:
Usually while(true) cause it's easiest to read. But I did like:

Code: Select all

`while len("the pope") == len("catholic"):`

That was used in some code on Nerd Paradise.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Contact:
Dingbats wrote:Usually while(true) cause it's easiest to read. But I did like:

Code: Select all

`while len("the pope") == len("catholic"):`

That was used in some code on Nerd Paradise.

There's of course this slight variant on that joke, and this related one. For personal code, I might actually use one of those.

torne
Posts: 98
Joined: Wed Nov 01, 2006 11:58 am UTC
Location: London, UK
for(;;) is the only one in C that's guaranteed not to cause a compiler warning. while ([some constant expression that's always true]) is a warning on many compilers under reasonable warning levels. In the codebase I'm employed to work on, we have "#define FOREVER for(;;)" as a result ;)

evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world
torne wrote:for(;;) is the only one in C that's guaranteed not to cause a compiler warning. while ([some constant expression that's always true]) is a warning on many compilers under reasonable warning levels. In the codebase I'm employed to work on, we have "#define FOREVER for(;;)" as a result

not that i'm suggesting using it, but goto should also work with no warnings.

SpitValve
Not a mod.
Posts: 5130
Joined: Tue Sep 26, 2006 9:51 am UTC
Location: Lower pork village
evilbeanfiend wrote:not that i'm suggesting using it, but goto should also work with no warnings.

No compiler warnings, just bugs...

evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world
SpitValve wrote:
evilbeanfiend wrote:not that i'm suggesting using it, but goto should also work with no warnings.

No compiler warnings, just bugs...

and after you posted that brilliant BASIC program too

canonical tcl is

Code: Select all

`while 1 {#do stuff } `

but there is also the event loop you can use so..

Code: Select all

`proc every {ms body} {eval \$body; after \$ms [info level 0]}pack [label .clock -textvar time]every 1000 {set ::time [clock format [clock sec] -format %H:%M:%S]}`

Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC
bitwiseshiftleft wrote:Map and filter aren't used much in Python because they're subsumed by list comprehensions, which are much easier to read if you're not a functional programmer

Reduce isn't used much in Python because for loops are more idiomatic.
I know. Using a lambda function inside map would just be silly, but when you already have a function, it's annoying to write the list-comprehension. map(ord, "xkcd") is nicer than [ord(c) for c in "xkcd"]. The second one is just a bunch of boilerplate.

Dingbats wrote:

Code: Select all

`while len("the pope") == len("catholic"):`
If you're going to do something silly like that, you might as well just use

Code: Select all

`while "the internet's a series of tubes":`

since string literals are evaluated to non-false in eg C++ and Python. In Python, for increased hilarity, you could even write

Code: Select all

`while "the internet" is not "a big truck":`

niko7865
Posts: 536
Joined: Sun Jan 21, 2007 11:47 am UTC
Location: All over washington state, USA
Contact:
the one in my signature is my favorite. Try it out in the terminal on a nix machine
21/m/athletic/white&nerdy/washington/straight/???

bitwiseshiftleft
Posts: 295
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford
Contact:
niko7865 wrote:the one in my signature is my favorite. Try it out in the terminal on a nix machine :D

As I mentioned above, that's a fork-bomb. Don't try it out on a nix machine unless you've set your ulimits carefully.

Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove
Torn Apart By Dingos wrote:
bitwiseshiftleft wrote:Map and filter aren't used much in Python because they're subsumed by list comprehensions, which are much easier to read if you're not a functional programmer

Reduce isn't used much in Python because for loops are more idiomatic.
I know. Using a lambda function inside map would just be silly, but when you already have a function, it's annoying to write the list-comprehension. map(ord, "xkcd") is nicer than [ord(c) for c in "xkcd"]. The second one is just a bunch of boilerplate.

Especially when:

Code: Select all

`def map( f, d ):  return [ord(c) for c in d]`

can be written -- why remove it? To break old code, or just to make the standard set of functions less dense?

Dingbats wrote:

Code: Select all

`while len("the pope") == len("catholic"):`
If you're going to do something silly like that, you might as well just use

Code: Select all

`while "the internet's a series of tubes":`

since string literals are evaluated to non-false in eg C++ and Python. In Python, for increased hilarity, you could even write

Code: Select all

`while "the internet" is not "a big truck":`

I personally like using:
assert(x && "There is something wrong with our xs");
many assert implementations display the assertion that failed -- so just embed a string in it.

parsonsb
301st Spartan (Overslept)
Posts: 229
Joined: Tue Jan 23, 2007 10:17 pm UTC
[code]for(int i=10;i<11;i--)[/code]

not infinite just will run for quite a while since i will wrap around to pos after a long time

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Contact:
Yakk wrote:I personally like using:
assert(x && "There is something wrong with our xs");
many assert implementations display the assertion that failed -- so just embed a string in it.

I do that all the time, sort of. Often the assertion is self explanatory... what am I going to write for assert(p); assert(p && "p shouldn't be null")? (Okay, sometimes something like "null is an invalid parameter value" or something would be slightly more helpful, but still somewhat questionable.)

What I *do* do all the time though is if I have a "this shouldn't ever execute" place I'll write "switch(e) { ... default: assert(0 && "e is an invalid value"); }". One of these days I should write a macro that will expand to that, but whatever...

Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Code: Select all

`assert(!"this should never execute");`

! operator -- makes your code more exciting!

dan
Posts: 62
Joined: Wed Aug 09, 2006 12:55 am UTC
! operator -- makes your code more exciting!

so in ruby:

Code: Select all

`while !!!!!!!!!!!!!!!!!!!!111 doputs "!!"end`

Peshmerga
Posts: 2061
Joined: Wed Oct 04, 2006 1:56 am UTC
Contact:
for(!!!!!!!!!!!!!!!true)
{
...
}
i hurd u liek mudkips???

Drostie
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC
No, no, no. The best way, by far, is to program the infinite loop in Chef.

Code: Select all

`Infinitely Baked PotatoInsert loop code between the [ square brackets ] below. If you don't mention the potato within that area, the loop is infinite. And infinitely tasty. Ingredients.1 potatoMethod.Pre-heat oven to 200 degrees Celsius.Bake the potato. [   ]Wait until baked.Serves 1.`

Pathway
Leon Sumbitches...?
Posts: 647
Joined: Sun Oct 15, 2006 5:59 pm UTC
parsonsb wrote:

Code: Select all

`for(int i=10;i<11;i--)`

not infinite just will run for quite a while since i will wrap around to pos after a long time

Code: Select all

`for(int i=10;i<11;i--){if (i < 0) i = 10; //CODEBLOCK}`

Re Chef: an actual programming language with highly amusing syntax? Or just teh funny?

SpitValve
Not a mod.
Posts: 5130
Joined: Tue Sep 26, 2006 9:51 am UTC
Location: Lower pork village
Pathway wrote:Re Chef: an actual programming language with highly amusing syntax? Or just teh funny?

The Internet Proves All

There's also one based on Shakespeare, one of Haiku, and one on Lemmings (sort of).

ARRG
Posts: 11
Joined: Wed Mar 21, 2007 8:19 pm UTC
Location: Switzerland
Contact:
Ruby has an infinite iterator

Code: Select all

`loop do...end`

But it's not funny.

In other languages I go for while(true) too.

heatsink
Posts: 86
Joined: Fri Jun 30, 2006 8:58 am UTC
Since we're looking at different languages...

[0..]::Integer

Note that although this is an infinite loop, it will run in finite time under some conditions. Try to do that in C!

cmacis
Posts: 754
Joined: Wed Dec 13, 2006 5:22 pm UTC
Contact:
An infinite loop in finite time? What lang is that and when did they create the machine that works on a continuous scale rather than the discrete tick we have?
li te'o te'a vei pai pi'i ka'o ve'o su'i pa du li no

Mathematician is a function mapping tea onto theorems. Sadly this function is irreversible.

QED is Latin for small empty box.

Ceci nâ€™est pas une [s]pipe[/s] signature.

Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:
heatsink wrote:Note that although this is an infinite loop, it will run in finite time under some conditions. Try to do that in C!

You can't do infinite things in a finite time. You're being silly.

cmacis
Posts: 754
Joined: Wed Dec 13, 2006 5:22 pm UTC
Contact:
You can pass through an infinite number of points in finite time.

Or can you.

Goes off to argue with zeno.
li te'o te'a vei pai pi'i ka'o ve'o su'i pa du li no

Mathematician is a function mapping tea onto theorems. Sadly this function is irreversible.

QED is Latin for small empty box.

Ceci nâ€™est pas une [s]pipe[/s] signature.

Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove
heatsink wrote:Since we're looking at different languages...

[0..]::Integer

Note that although this is an infinite loop, it will run in finite time under some conditions. Try to do that in C!

That happens all of the time when you toss an optimizer at code. :p

Now, summing 1 to infinity and getting -1/12, that is hard in C.