#354: Strangest Loop Competition, 2011 Edition

107 views
Skip to first unread message

Weiqi Gao

unread,
Jun 17, 2011, 8:12:48 AM6/17/11
to The Java Posse
I just finished listening to #354, in which Dick Wall calls for another
round of the Strangest Loop Competition to win tickets to the Strange
Loop 2001 Conference in St. Louis, MO, USA.

The stipulation this year is that the loop must be in a language other
than Java.

Here's my entry (it won't be the strangest for sure) to get the balls
rolling. It's a loop in C++:

for (int i = 0; i < 10; i++) {}

It doesn't do anything except to burn cycles, but it is a loop in a
language other than Java. ;)

--
Weiqi Gao
weiq...@gmail.com
http://www.weiqigao.com/blog/

Kirk

unread,
Jun 17, 2011, 8:23:54 AM6/17/11
to java...@googlegroups.com
for (;true;) {}

shorter and also burns cycles ;-)

Kirk

> --
> You received this message because you are subscribed to the Google Groups "The Java Posse" group.
> To post to this group, send email to java...@googlegroups.com.
> To unsubscribe from this group, send email to javaposse+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
>

Joseph Ottinger

unread,
Jun 17, 2011, 8:31:04 AM6/17/11
to java...@googlegroups.com
And burns way more!

while(true) {} // less steps, same number of characters and cycles,
eat it BIATCH!

--
Joseph B. Ottinger
http://enigmastation.com

Jonathan Fuerth

unread,
Jun 17, 2011, 9:22:34 AM6/17/11
to java...@googlegroups.com

If shorter is indeed stranger and C "Isn't Java," I can one-up Kirk:

for (;;);

Or

GOTO: goto GOTO;

(But of course this second one doesn't also work in Java, so it's hardly pushing the rules. Where's the fun in that?)

-Jonathan

Ricky Clarkson

unread,
Jun 17, 2011, 9:25:30 AM6/17/11
to java...@googlegroups.com
<caine>Not many people know this</caine>, but:

throw null; is valid Java, Scala and C#.

The above is the one use of XML I wouldn't mind seeing in 20 years time.

Kirk

unread,
Jun 17, 2011, 9:32:03 AM6/17/11
to java...@googlegroups.com
good one!!! I considers for(;;) {} but I wasn't sure what the conditional would be.

Kirk

Cédric Beust ♔

unread,
Jun 17, 2011, 1:14:38 PM6/17/11
to java...@googlegroups.com
void foo() {
  try {}
  finally { foo(); }
}

I wish I could take credit for this but it's actually code that I saw on irc not long ago.

-- 
Cédric

Kevin Wright

unread,
Jun 17, 2011, 1:22:38 PM6/17/11
to java...@googlegroups.com


2011/6/17 Cédric Beust ♔ <ced...@beust.com>
I think everyone's missing the point here that it should NOT be Java.

I'd reuse duffs device, but that would be a bit cheeky after submitting it last year.
So, without further ado, I present you with the "sleep sort" algorithm, implemented in Bash:

#!/bin/bash
function f() {
    sleep 
"$1"
    echo 
"$1"
}
while [ -"$1" ]
do
    f 
"$1" &
    shift
done
wait


example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7
 

A strange loop AND concurrency, what's not to like?

I can't take credit though, the original is to be found on 4chan here:

James Iry also produced a fun variant using Scala actors here:



--
Kevin Wright

gtalk / msn : kev.lee...@gmail.com
mail: kevin....@scalatechnology.com
vibe / skype: kev.lee.wright
quora: http://www.quora.com/Kevin-Wright
twitter: @thecoda

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra

Eric Jablow

unread,
Jun 17, 2011, 2:45:18 PM6/17/11
to The Java Posse
use Lingua::Romana::Perligata;

dum unum
sic
This tum is tum a tum very tum strange tum loop inquementum tum
novumversum egresso scribe.
cis.

Respectfully,
Eric Jablow

Alex Miller

unread,
Jun 17, 2011, 3:05:57 PM6/17/11
to The Java Posse
Thanks Weiqi! I've been meaning to post this myself. We'll give away
2 tickets (either single ticket to two people or 2 tickets to 1
person).

You can see last year's contest here:
http://groups.google.com/group/javaposse/browse_thread/thread/7ac69c92a833c873/a488132317b728ed

More info on Strange Loop: http://thestrangeloop.com

Alex Miller
aka Dr. Strange Loop
> weiqi...@gmail.comhttp://www.weiqigao.com/blog/

Jon Kiparsky

unread,
Jun 17, 2011, 5:17:03 PM6/17/11
to The Java Posse
Allow me to submit:

Douglas Hofstadter.

(I'm pretty sure he's not written in Java...)

On Jun 17, 3:05 pm, Alex Miller <a...@puredanger.com> wrote:
> Thanks Weiqi!  I've been meaning to post this myself.  We'll give away
> 2 tickets (either single ticket to two people or 2 tickets to 1
> person).
>
> You can see last year's contest here:http://groups.google.com/group/javaposse/browse_thread/thread/7ac69c9...

Danno

unread,
Jun 17, 2011, 5:58:58 PM6/17/11
to The Java Posse
Language: Beatnik (http://esolangs.org/wiki/Beatnik)

Can StrangeLoop convolution us?

(disassembled:
#5 - push next
#14 - blatant pandering
#16 - Jump back N if not zero
#2 - n
)
--Danno
> weiqi...@gmail.comhttp://www.weiqigao.com/blog/

Vince O'Sullivan

unread,
Jun 18, 2011, 7:59:07 AM6/18/11
to The Java Posse
On Jun 17, 1:12 pm, Weiqi Gao <weiqi...@gmail.com> wrote:
> ...win tickets to the Strange Loop 2001 Conference in St. Louis, MO, USA.

When is it on?

Weiqi Gao

unread,
Jun 18, 2011, 8:20:50 AM6/18/11
to java...@googlegroups.com

Michael Barker

unread,
Jun 18, 2011, 9:00:41 AM6/18/11
to java...@googlegroups.com
Here's my strange loop, in brainf***.

++++++++++>
++++++++++>
++++++++++++++++++++++++++++++++++++++++++++++++<
[>.+<<.>-]

Guess what it does?

Mike

P.S. Won't be able to make strange loop, so won't need a ticket.

Weiqi Gao

unread,
Jun 18, 2011, 9:08:26 AM6/18/11
to java...@googlegroups.com
A program in the same vein, but in Scheme (definitely a non-Java language):

(letrec ((f (lambda ()
(f))))
(f))

which, if you don't count the parentheses, is quite short.

Unlike its Java counterpart, any self-respecting properly tail-recursive
Scheme implementation will run this infinite loop indefinitely without
blowing the stack.

Just don't run this program while in a quite meeting, or the fan will
kick on and the presenter will ask "Is an airplane taking off?"

--
Weiqi

> >>>>> weiq...@gmail.com <mailto:weiq...@gmail.com>
> >>>>> http://www.weiqigao.com/blog/

Paul King

unread,
Jun 18, 2011, 9:17:09 AM6/18/11
to java...@googlegroups.com
// Groovy: collect loops through strange
def strange = [A:'Miller', l:'Strange', e:'Louis', x:'St.']
assert 2011 == strange.collect{ k, v -> k.bytes[0] * v.size() }.sum()

Casper Bang

unread,
Jun 18, 2011, 9:45:50 AM6/18/11
to The Java Posse
Probably the shortest and most "optimal" strange loop, can be done in
classic x86 assembly:

jmp $ ;Immediate unconditional jump to the address this very
instruction resolves to

The interoperability of C/C++ means we could do it almost as terse in
these languages:

__asm { jmp $ } //Immediate unconditional jump to the address this
very instruction resolves to

JuanManuel Gimeno Illa

unread,
Jun 18, 2011, 11:49:42 AM6/18/11
to java...@googlegroups.com
An strangely infinite loop:

#include <stdio.h>

int main(int argc, char* argv[]) {
    int i, a[1];
    for (i=0; i<=1; i++) {
        a[i] -= 4;
        printf("x");
    }
}

Jon Kiparsky

unread,
Jun 18, 2011, 4:29:21 PM6/18/11
to java...@googlegroups.com
Infiinite?

Here's the output on my machine:

/Users/jon:502 $ gcc strange_loop.c
[jon: Sat Jun 18 16:24]
/Users/jon:503 $ ./a.out
xx[jon: Sat Jun 18 16:24]

Jon Kiparsky

unread,
Jun 18, 2011, 4:53:20 PM6/18/11
to java...@googlegroups.com
(here's a repaired version)


#include <stdio.h>

int main(int argc, char* argv[]) {
    int i, a[1];
     
   long j =  sizeof(i);


    for (i=0; i<=j; i++) {
      printf("x");
      a[i]-=j;
    }
}


JuanManuel Gimeno Illa

unread,
Jun 18, 2011, 5:07:32 PM6/18/11
to java...@googlegroups.com
Mine wasn't portable :-(

Thanks.

Juan Manuel

Jon Kiparsky

unread,
Jun 18, 2011, 5:20:38 PM6/18/11
to java...@googlegroups.com
I knew exactly what you were doing when I saw it - I had a bug like that when I was first learning C, except the "i" variable in this case was in the main, and the array was in a function - I overshot the end of the array, and wrote a newline onto the i character. Took me an hour to figure out why I was always getting 48 as the value of i...  was trying to debug the main, but the problem was in the function... :)

Kevin Wright

unread,
Jun 18, 2011, 6:30:54 PM6/18/11
to java...@googlegroups.com
Sadly not tested, j-intercal didn't work for me out of the box... and it wasn't important enough to spend time fixing!

(1) PLEASE DO COME FROM (2)
DO ,1 <- #13
PLEASE DO ,1 SUB #1 <- #238
DO ,1 SUB #2 <- #108
DO ,1 SUB #3 <- #112
DO ,1 SUB #4 <- #0
DO ,1 SUB #5 <- #64
DO ,1 SUB #6 <- #194
DO ,1 SUB #7 <- #48
PLEASE DO ,1 SUB #8 <- #22
DO ,1 SUB #9 <- #248
DO ,1 SUB #10 <- #168
DO ,1 SUB #11 <- #24
DO ,1 SUB #12 <- #16
DO ,1 SUB #13 <- #162
(2) DO READ OUT ,1
PLEASE GIVE UP

I also feel kinda wrong putting in a second entry.  But it's intercal, how could I resist?  I'm in the wrong county for the tickets anyway.


p.s. For those who were wondering, it's the intercal equivalent of:

10 PRINT "Hello, World!"
20 GOTO 10



On 18 June 2011 22:20, Jon Kiparsky <jon.ki...@gmail.com> wrote:
I knew exactly what you were doing when I saw it - I had a bug like that when I was first learning C, except the "i" variable in this case was in the main, and the array was in a function - I overshot the end of the array, and wrote a newline onto the i character. Took me an hour to figure out why I was always getting 48 as the value of i...  was trying to debug the main, but the problem was in the function... :)

--
You received this message because you are subscribed to the Google Groups "The Java Posse" group.
To view this discussion on the web visit https://groups.google.com/d/msg/javaposse/-/OQDvs9QTYoIJ.

To post to this group, send email to java...@googlegroups.com.
To unsubscribe from this group, send email to javaposse+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.



--

Ricky Clarkson

unread,
Jun 18, 2011, 6:45:28 PM6/18/11
to java...@googlegroups.com
I can only assume that you are from the future and reporting on COBOL THREE, known colloquially as Java.Next.  It is reported to be the 4th language to, on its deathbed, exclaim "Ok, I give in, you can have lambdas!".

If you think that was bad, you should see the stuff I deleted before hitting Send.

Kevin Wright

unread,
Jun 18, 2011, 7:32:34 PM6/18/11
to java...@googlegroups.com
On 18 June 2011 23:45, Ricky Clarkson <ricky.c...@gmail.com> wrote:
I can only assume that you are from the future and reporting on COBOL THREE, known colloquially as Java.Next.  It is reported to be the 4th language to, on its deathbed, exclaim "Ok, I give in, you can have lambdas!".


What were the other two then?

ushac

unread,
Jul 11, 2011, 5:39:37 AM7/11/11
to The Java Posse
Here's my contribution:


enum State { INIT, RUNNING, STOPPED };

struct Foo { State mMyState : 2; };

int main(int argc, char* argv[]) {
struct Foo tMyFoo;
while (tMyFoo.mMyState != STOPPED) {
tMyFoo.mMyState = STOPPED;
}
}


This turns into a infinite loop. Not sure how portable it is though.
The quirky thing here is the bit field. Since there are only three
enum vals, 2 bits should be enough as that can store 4 different
values, right?
But enums are often realized as signed integers. That usually means
the the value of STOPPED will be int value 2, of 10b. However, in
order to do the comparison, the value in the variable mMyState will be
cast up to a full integer. That casting uses signed extension, and
since the highest order bit of the 2 bit value 10b is a 1, it will be
interpreted as a negative two's complement value and be 1-padded.
Hence the comparison will fail. Isn't C++ fun? :)

PS. Won't be able to go to the conference.

JuanManuel Gimeno Illa

unread,
Jul 13, 2011, 3:46:56 AM7/13/11
to java...@googlegroups.com
I forgot to add that I won't be able to go to the conference :-(

Casper Bang

unread,
Jul 13, 2011, 4:57:07 AM7/13/11
to The Java Posse
> This turns into a infinite loop. Not sure how portable it is though.

I can reproduce this with VS2008, but not with gcc 4.5.2. Since you
are not initializing tMyFoo.mMyState I supposed, even in gcc, there's
still a chance the loop will never commence due to the random bit
pattern of tMyFoo.mMyState matching STOPPED?!

ushac

unread,
Jul 14, 2011, 9:34:03 AM7/14/11
to The Java Posse
Hm, nice find!

I'm thinking gcc is using unsigned ints for enums or they don't do
signed extension of bit fields. I'll have to experiment a bit. :)

/Erik

Sam Reid

unread,
Jul 16, 2011, 4:55:24 PM7/16/11
to The Java Posse
I know Java code is off limits for this round, but I just wrote a line
of Java code that feels too much like a strange loop:

class Lattice<T extends Lattice<T>>

It's an emulation of the self-type pattern in Java.

Sam

Alex Miller

unread,
Jul 17, 2011, 3:47:48 PM7/17/11
to The Java Posse
Or the more classic example in the JDK itself:

class Enum<E extends Enum<E>>

http://download.oracle.com/javase/6/docs/api/java/lang/Enum.html

John

unread,
Aug 16, 2011, 2:31:05 PM8/16/11
to The Java Posse
Infinite scala loop with two iterators going in opposite directions:

object OppositeIterators extends Application {
class Infinite(delta: Int) extends Iterator[BigInt] {
var cur: BigInt = 0
def hasNext = true
def next = { cur += delta; cur }
}
val negative = new Infinite(-1)
val positive = new Infinite(1)
positive.filter(_ + negative.next != 0).next
}

On Jun 17, 2:05 pm, Alex Miller <a...@puredanger.com> wrote:
> Thanks Weiqi!  I've been meaning to post this myself.  We'll give away
> 2 tickets (either single ticket to two people or 2 tickets to 1
> person).
>
> You can see last year's contest here:http://groups.google.com/group/javaposse/browse_thread/thread/7ac69c9...
>
> More info on Strange Loop:  http://thestrangeloop.com
>
> Alex Miller
> aka Dr. Strange Loop
>
> On Jun 17, 7:12 am, Weiqi Gao <weiqi...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I just finished listening to #354, in which Dick Wall calls for another
> > round of the Strangest LoopCompetitionto win tickets to the Strange
Reply all
Reply to author
Forward
0 new messages