Saturday, May 02, 2009

Self-printing Groovy Program

It always amazes me when I read about how people write a C program that print itself. The self-printing program, or more formally, quine, prints out its source code.

Having used Groovy for a while, I wonder if there is any self-printing Groovy program on the Internet. After some googling around, I found some Java programs on this page. After spending about 15 minutes, I came up with what I think is the first self-printing Groovy program to date. I used Bertram Felgenhauer's 2nd example as my starting point. Here is my version in Groovy (the code is written in one line):

class S{public static void main(a){def s="class S{public static void main(a){def s=;char c=34;println(s.substring(0,41)+c+s+c+s.substring(41));}}";char c=34;println(s.substring(0,41)+c+s+c+s.substring(41));}}

See if you can make it shorter.

Updates



Chrigel suggested the removal of "public". I have to adjust the parameter to substring() too. Here is a shorter version (195 bytes):

class S{static void main(a){def s="class S{static void main(a){def s=;char c=34;println(s.substring(0,34)+c+s+c+s.substring(34));}}";char c=34;println(s.substring(0,34)+c+s+c+s.substring(34));}}


paulk's version is 97 bytes:

def s="def s=;char c=34;println s[0..5]+c+s+c+s[6..-1]";char c=34;println s[0..5]+c+s+c+s[6..-1]


paulk's 2nd version is 46 bytes:

s="s=%c%s%c;printf s,34,s,34";printf s,34,s,34


paulk's 3rd version is 42 bytes. Keep going, paulk!

s='s=%c%s%1$c;printf s,39,s';printf s,39,s


Gavin Grover found a 38-char solution at http://golf.shinh.org/p.rb?Quine:

printf _='printf _=%c%s%1$c,39,_',39,_


Are we approaching the limit?

12 comments:

Chrigel said...

don't need the "public" keyword I think ...

Tai Siew Joon said...

Updated. Thanks for your comment, Chrigel.

paulk_asert said...

You can also leave out void or go whole hog and leave out the class and main methods:

def s="def s=;char c=34;println s[0..5]+c+s+c+s[6..-1]";char c=34;println s[0..5]+c+s+c+s[6..-1]

paulk_asert said...

you can remove def and the EOL as well to get 82 chars:

s="s=;char c=34;print s[0,1]+c+s+c+s[2..-1]";char c=34;print s[0,1]+c+s+c+s[2..-1]

or borrowing from inspiration from the wikipedia page you reference, get down to 46 chars using printf:

s="s=%c%s%c;printf s,34,s,34";printf s,34,s,34

paulk_asert said...

Here's another one (50 chars so not as short) but might be easier to grok:

s="s=%s;printf s,s.inspect()";printf s,s.inspect()

paulk_asert said...

Here is a way to prune 4 more chars off to make it 42:

s='s=%c%s%1$c;printf s,39,s';printf s,39,s

Tai Siew Joon said...

A wild idea just flash over my mind. I think I've found the shortest self-printing Groovy program. It is an empty file (0 byte). Try to beat that, paulk, hehehe :D

paulk_asert said...

Longer again at 47 chars but a different flavor (stolen from Gavin Grover on the Groovy mailing list Aug 2006):

evaluate s='char q=39;print"evaluate s=$q$s$q"'

Also, pasted the best four here:

http://rosettacode.org/wiki/Quine#Groovy

paulk_asert said...

Hi Tai, LOL. Yes, I was only trying for smallest non-trivial solution.

Anonymous said...

I found this 38-char job at http://golf.shinh.org/p.rb?Quine

printf _='printf _=%c%s%1$c,39,_',39,_

posted by murky-satyr on 2nd nov 2008.

Could someone else post it on http://rosettacode.org/wiki/Quine#Groovy as I don't have an IDE to make the colors.

Tai Siew Joon said...

Gavin, that's an interesting URL. I've added it to my bookmarks.

gorlok said...

Bravo, now Groovy looks like Perl :(
.
.
.
But, nice and interesting post :)