[ Poll ] Infinite loops

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

Moderators: phlip, Moderators General, Prelates

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

[ Poll ] Infinite loops

Postby warriorness » Wed Mar 28, 2007 5:18 pm UTC

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

User avatar
cmacis
Posts: 754
Joined: Wed Dec 13, 2006 5:22 pm UTC
Location: Leeds or Bradford, Thessex
Contact:

Postby cmacis » Wed Mar 28, 2007 5:24 pm UTC

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

Postby demon » Wed Mar 28, 2007 5:27 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
Location: Madison, WI
Contact:

Postby EvanED » Wed Mar 28, 2007 6:14 pm UTC

while(true).

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

User avatar
simen
Posts: 166
Joined: Wed Feb 21, 2007 7:02 pm UTC
Contact:

Postby simen » Wed Mar 28, 2007 6:22 pm UTC

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

Postby Rysto » Wed Mar 28, 2007 7:35 pm UTC

Some people like

Code: Select all

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


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

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

Postby SpitValve » Wed Mar 28, 2007 7:55 pm UTC

Code: Select all

10 PRINT "LOOK AROUND YOU"
20 GOTO 10

User avatar
bitwiseshiftleft
Posts: 295
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford
Contact:

Postby bitwiseshiftleft » Wed Mar 28, 2007 8:37 pm UTC

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.

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

Postby Yakk » Wed Mar 28, 2007 8:59 pm UTC

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)

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

Postby Andrew » Wed Mar 28, 2007 10:59 pm UTC


User avatar
Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

Postby Torn Apart By Dingos » Wed Mar 28, 2007 11:03 pm 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
Location: Madison, WI
Contact:

Postby EvanED » Wed Mar 28, 2007 11:10 pm UTC

Andrew wrote:Like this.


Haha... that's great

Stig Hemmer
Posts: 30
Joined: Wed Feb 28, 2007 4:46 pm UTC
Location: Trondheim, Norway

Postby Stig Hemmer » Wed Mar 28, 2007 11:43 pm UTC

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.

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

Postby notzeb » Thu Mar 29, 2007 2:20 am UTC

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

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

Postby Yakk » Thu Mar 29, 2007 3:49 am UTC

Then what was learned from Python will be taken and used to build another language that won't suck.

:)

User avatar
bitwiseshiftleft
Posts: 295
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford
Contact:

Postby bitwiseshiftleft » Thu Mar 29, 2007 4:36 am UTC

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.

User avatar
Dingbats
Posts: 921
Joined: Tue Mar 20, 2007 12:46 pm UTC
Location: Sweden
Contact:

Postby Dingbats » Thu Mar 29, 2007 5:36 am UTC

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
Location: Madison, WI
Contact:

Postby EvanED » Thu Mar 29, 2007 5:53 am UTC

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. ;-)

User avatar
torne
Posts: 98
Joined: Wed Nov 01, 2006 11:58 am UTC
Location: London, UK

Postby torne » Thu Mar 29, 2007 9:53 am UTC

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 ;)

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Postby evilbeanfiend » Thu Mar 29, 2007 10:11 am UTC

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.

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

Postby SpitValve » Thu Mar 29, 2007 10:14 am UTC

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


No compiler warnings, just bugs...

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Postby evilbeanfiend » Thu Mar 29, 2007 10:32 am UTC

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 :wink:

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]}

User avatar
Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

Postby Torn Apart By Dingos » Thu Mar 29, 2007 11:16 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":

User avatar
niko7865
Posts: 536
Joined: Sun Jan 21, 2007 11:47 am UTC
Location: All over washington state, USA
Contact:

Postby niko7865 » Thu Mar 29, 2007 5:20 pm UTC

the one in my signature is my favorite. Try it out in the terminal on a nix machine :D
21/m/athletic/white&nerdy/washington/straight/???
Image

User avatar
bitwiseshiftleft
Posts: 295
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford
Contact:

Postby bitwiseshiftleft » Thu Mar 29, 2007 6:56 pm UTC

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.

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

Postby Yakk » Thu Mar 29, 2007 7:39 pm UTC

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.

User avatar
parsonsb
301st Spartan (Overslept)
Posts: 229
Joined: Tue Jan 23, 2007 10:17 pm UTC

Postby parsonsb » Thu Mar 29, 2007 8:07 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
Location: Madison, WI
Contact:

Postby EvanED » Thu Mar 29, 2007 8:08 pm UTC

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...

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

Postby Yakk » Thu Mar 29, 2007 8:21 pm UTC

Code: Select all

assert(!"this should never execute");


! operator -- makes your code more exciting!

User avatar
dan
Posts: 62
Joined: Wed Aug 09, 2006 12:55 am UTC

Postby dan » Thu Mar 29, 2007 11:48 pm UTC

! operator -- makes your code more exciting!


so in ruby:

Code: Select all

while !!!!!!!!!!!!!!!!!!!!111 do
puts "!!"
end

User avatar
Peshmerga
Mad Hatter
Posts: 2061
Joined: Wed Oct 04, 2006 1:56 am UTC
Contact:

Postby Peshmerga » Fri Mar 30, 2007 12:28 am UTC

for(!!!!!!!!!!!!!!!true)
{
...
}
i hurd u liek mudkips???

User avatar
Drostie
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby Drostie » Fri Mar 30, 2007 7:17 am UTC

No, no, no. The best way, by far, is to program the infinite loop in Chef.

Code: Select all

Infinitely Baked Potato
Insert 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 potato

Method.
Pre-heat oven to 200 degrees Celsius.

Bake the potato.

[   ]

Wait until baked.

Serves 1.

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

Postby Pathway » Sat Mar 31, 2007 8:18 am 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?

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

Postby SpitValve » Sat Mar 31, 2007 9:27 am UTC

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).

User avatar
ARRG
Posts: 11
Joined: Wed Mar 21, 2007 8:19 pm UTC
Location: Switzerland
Contact:

Postby ARRG » Sat Mar 31, 2007 8:04 pm UTC

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

Postby heatsink » Sun Apr 01, 2007 2:30 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!

User avatar
cmacis
Posts: 754
Joined: Wed Dec 13, 2006 5:22 pm UTC
Location: Leeds or Bradford, Thessex
Contact:

Postby cmacis » Sun Apr 01, 2007 11:06 am UTC

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.

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

Postby Andrew » Sun Apr 01, 2007 12:29 pm UTC

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.

User avatar
cmacis
Posts: 754
Joined: Wed Dec 13, 2006 5:22 pm UTC
Location: Leeds or Bradford, Thessex
Contact:

Postby cmacis » Sun Apr 01, 2007 12:49 pm UTC

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.

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

Postby Yakk » Sun Apr 01, 2007 5:04 pm UTC

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.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests