Retrieved from ""

A quine program with a twist

2010-03-26 11:35:08

keywords: offbeat, programming

From Wikipedia:

In computing, a quine is a computer program which produces a copy of its own source code as its only output.

Quines like that are known to exist in every Turing complete language. Examples can be found in the Wikipedia article linked above and elsewhere on the net.

Then there are the programmers who are not satisfied with just regular exceptionality.

About six months ago, ku-ma-me, a japanese blogger, published this Ruby code:

# ruby
l=92.chr;eval s="s=s.dump[r=1..-2].gsub(/("+l*4+"){4,}(?!\")/){|t|'\"+l*%d+\"'%(t
.size/2)};5.times{s=s.dump[r]};puts\"# python\\nprint(\\\"# perl\\\\nprint(\\\\\\
\"# lua"+l*4+"nprint("+l*7+"\"(* ocaml *)"+l*8+"nprint_endline"+l*15+"\"-- haskel
l"+l*16+"nimport Data.List;import Data.Bits;import Data.Char;main=putStrLn("+l*31
+"\"/* C */"+l*32+"n#include<stdio.h>"+l*32+"nint main(void){char*s[501]={"+l*31+
"\"++intercalate"+l*31+"\","+l*31+"\"(c(tail(init(show("+l*31+"\"/* Java */"+l*32
+"npublic class QuineRelay{public static void main(String[]a){String[]s={"+l*31+"
<-]+++++++++>>++++++++++"+l*31+"\"++(concat(snd(mapAccumL h 2("+l*31+"\"110"+l*31
+"\"++g(length s)++"+l*31+"\"22111211100111112021111102011112120012"+l*31+"\"++co
ncatMap("+l*32+"c->let d=ord c in if d<11then"+l*31+"\"21002"+l*31+"\"else"+l*31+
"\"111"+l*31+"\"++g d++"+l*31+"\"22102"+l*31+"\")s++"+l*31+"\"2100211101012021122
*63+"\""+l*64+"n"+l*63+"\"};int i=0;for(;i<94;i++)System.out.print(s[i]);}}"+l*31
+"\")))))++"+l*31+"\",0};int i=0;for(;s[i];i++)printf("+l*63+"\"%s"+l*63+"\",s[i]
);puts("+l*63+"\""+l*63+"\");return 0;}"+l*31+"\");c s=map("+l*32+"s->"+l*31+"\""
+l*63+"\""+l*31+"\"++s++"+l*31+"\""+l*63+"\""+l*31+"\")(unfoldr t s);t[]=Nothing;
t s=Just(splitAt(if length s>w&&s!!w=='"+l*31+"\"'then 501else w)s);w=500;f 0=Not
hing;f x=Just((if x`mod`2>0then '0'else '1'),x`div`2);g x= reverse (unfoldr f x);
h p c=let d=ord c-48in(d,replicate(abs(p-d))(if d<p then '<'else '>')++"+l*31+"\"
."+l*31+"\");s="+l*31+"\"# ruby"+l*32+"n"+l*31+"\"++"+l*31+"\"l=92.chr;eval s=\"+
l*7+"\")"+l*4+"n\\\\\\\")\\\")\"########### (c) Yusuke Endoh, 2009 ###########\n"

(Apparently it has to be saved without line breaks.)

So what will happen if you run that code? The shell output very much speaks for itself:

$ ruby QuineRelay.rb >
$ python >
$ perl > QuineRelay.lua
$ lua QuineRelay.lua >
$ ocaml > QuineRelay.hs
$ runghc QuineRelay.hs > QuineRelay.c
$ gcc -Wall -o QuineRelay QuineRelay.c && ./QuineRelay >
$ javac && java QuineRelay >
$ beef >
$ wspace > QuineRelay.unl
$ unlambda QuineRelay.unl > QuineRelay2.rb
$ diff QuineRelay.rb QuineRelay2.rb

The original article clarifies the employed versions of the involved compilers and interpreters.

According to the author of this piece of code, creating it was "easier than it looks", he says he finished it in under three hours. The result is truly fascinating regardless.


Please DO NOT enter anything into the next three fields, they are used as bait for automated entries. If any of the following fields is not left empty, the comment will not be saved!

End of protection - from here on, you can safely enter data.

Comments may be formatted using Markdown.

emphasis *emphasis* or _emphasis_
strong emphasis **strong emphasis** or __strong emphasis__
new paragraph insert an empty line


> quote
  • unordered
  • list
* unordered
* list
  1. ordered
  2. list
1. ordered
2. list
weblink [weblink](
Note: You can only post weblinks if you are authenticated via OpenID.
More and longer examples are available at Daring Fireball.
2010-03-26 14:19:46

Wow, soviel Zeit wie der würd ich auch gern mal haben ;)

Julian F.
Julian F. (via
2010-03-26 15:36:18

Soll ja angeblich gar nicht so lange gedauert haben. Wie er es gemacht hat ist mir trotzdem ziemlich schleierhaft.