1
\$\begingroup\$

The challenge is to write a fake infinite loop in any imperative programming language.

At first sight, the code should seem not to halt at all.

As a naive example:

int f(int x)
{
    return f(4.0);
}

int f(double x)
{
    return 0;
}

But remember
You're not allowed to use overflows:

int f(int x)
{
    if(x < 0) return -1;
    return f(x*x);
}

is illegal. When the number exceeds the integer limit, the function will stop. It's a good trick. But not good enough :)

The answer with the highest votes wins! Duration is 1 week.

\$\endgroup\$
5
  • \$\begingroup\$ @m.buettner You're right. I changed it to popularity contest. Anything else is good enough. Overflows is the first thing that pops into one's mind (as well as mine). \$\endgroup\$
    – padawan
    Jul 7, 2014 at 21:29
  • \$\begingroup\$ I'd suggest maybe broadening the anti-overflow rule to just say you can't break out of the loop using an error/exception. \$\endgroup\$
    – histocrat
    Jul 7, 2014 at 21:31
  • 1
    \$\begingroup\$ Nevertheless, I don't think this adds a lot over this challenge because I think in most cases you will be able to find some kind of termination condition that you just need to invert to fulfil the condition for this challenge. I'd vote to close as duplicate (instead of my now wrong close reason "unclear"), but I can't close vote again if I retract, so I guess the wrong reason stays for now. \$\endgroup\$ Jul 7, 2014 at 21:35
  • 3
    \$\begingroup\$ It's unclear too. Neither of the two examples looks remotely like an infinite loop. \$\endgroup\$ Jul 7, 2014 at 21:38
  • \$\begingroup\$ For the record, I voted to close as duplicate rather than as unclear, so I don't see any point in trying to improve the clarity, but since I see that someone has voted to reopen: the second example is still unclear IMO. What if I call f(0)? (And the first example still wouldn't fool anyone with an IQ above 90 and at least two hours of programming experience). \$\endgroup\$ Jul 8, 2014 at 8:23

1 Answer 1

5
\$\begingroup\$

Bash

trap 'exec "$0"' exit

It re-execs itself when it should exit. So it will never really exit.

It doesn't have the shebang. So you must run it like bash a.sh. And $0 will probably not be something executable.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.