|
|
|
|
|
|
|
|
|
The following collection of self-reproducing code in various programming languages was collected by Joe Miller and myself. We started from some standard examples in C and tried to generalize to other languages.
Near the bottom, you can find code that is simultaneously selfish in more than one language! So far Joe has dual selfish C-tcl and C-lisp code. Anyone care to give it a try? Particularly challenging would be, say, Fortran-TeX or, even worse, three languages.
I haven't updated this page in a long time. For a much larger collection of selfish code, you should check out Gary Thompson's Quine Page, which contains many of the programs listed here, among others.
Chris Hruska, chruska at math.uchicago.eduThe following is a collection of ``selfish'' programs. Selfish programs are self-reproducing programs; programs that produce their own source code. Because of the diversity of languages I don't have a strict definition of selfish. In a functional language a selfish program usually evaluates to itself. In an imperative language a selfish program usually prints itself out. There is no reason to make these hard and fast rules though. I included anything that was short, clever, or interesting. There are a huge number of languages and almost all of them admit selfish programs. This is by no means a canonical list, but perhaps someday it will be.
Joe Miller, millerj7 at indiana.edumain(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,3
4,c,34);}
Author: Unknown
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Author: Thad Smith#include<stdio.h>Author: Joe Miller
main(){char*c="\"#include<stdio.h>%cmain(){char*c=%c%c%c%.102s%cn%c
;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);} ";printf(c+2,c[10
2],c[1],*c,*c,c,*c,c[1]);exit(0);}
#define T(a) main(){printf(a,#a);}
T("#define T(a) main(){printf(a,#a);}
T(%s)")
Author: Dan Hoey/**/char q='"',*a="*//**/char q='%c',*a=%c%s%c*/};)b(stup;]d[b=]d-852Author: smr
[b)--d(elihw;)q,a,q,q,2+a,b(ftnirps{)(niam;031=d tni;]952[b,",b[259];
int d=130;main(){sprintf(b,a+2,q,q,a,q);while(d--)b[258-d]=b[d];puts(
b);}/*c%s%c%=a*,'c%'=q rahc/**//*"=a*,'"'=q rahc/**/
Pretty short, eh?
import java.text.*;class a{public static void main(String x[]){char b[]={34};
char c[]={123};String s[]=new String[3];s[0]="import java.text.*;class a{2}
public static void main(String x[]){2}char b[]={2}34};char c[]={2}123};
String s[]=new String[3];s[0]={1}{0}{1};s[1]=new String(b);s[2]=new String(c);
System.out.println(MessageFormat.format(s[0],s));}}";s[1]=new String(b);s[2]=
new String(c);System.out.println(MessageFormat.format(s[0],s));}}
{ One single line }
CONST T=';BEGIN WRITE(#67#79#78#83#84#32#84
#61#39,T,#39,T)END.';BEGIN WRITE(#67#79#78#83
#84#32#84#61#39,T,#39,T)END.
Author: Oliver HeenCONST A=';VAR S,T:STRING;BEGIN READLN(S);READLN(T); WRITELN(S+T=#67#79#78#83#84#32#65#61#39+A+#39+A)END.'
;VAR S,T:STRING;BEGIN READLN(S);READLN(T);WRITELN(S+T=#67#79#78#83#84#32#65#61#39+A+#39+A)END.
set x {set x {@}; regsub @ x; set x}; regsub @ x; set x
Author: Joe Miller
join [split a{a} a] {join [split a{a} a] }
Author: Joe Miller
join {{} { }} {join {{} { }} }
TAuthor: Chris Hruska
((LAMBDA (X) (LIST X (LIST 'QUOTE X))) '(LAMBDA (X) (LIST X (LIST 'QUAuthor: Joe Miller
OTE X))))
((LAMBDA (X) `(,X ',X)) '(LAMBDA (X) `(,X ',X)))
character*999s,p
datas/'''/end(6x,a17/3(5x,a1,a66/),6x,a40/6x,a66/6x,a3)character*
'999s,p datas/p=s(64:70)//s(:1)//s(:1)//s(:176)//s(:2)write(*,s(6:5
'6))s(48:),p,p,s,p(67:),s,p(133:),s(71:),s(111:),s(3:)'/
p=s(64:70)//s(:1)//s(:1)//s(:176)//s(:2)
write(*,s(6:56))s(48:),p,p,s,p(67:),s,p(133:),s(71:),s(111:),s(3:)
end
10 LISTAuthor: Chris Hruska
10 LIST : REM MER : TSIL 01Author: Chris Hruska
a60chr33 3334):d60chr33 3358)+chr33 3399)+chr33 3336)+chr33 3361):b60"a60chr33 3334):d33br>=chr33 3358)+chr33 3399)+chr33 3336)+chr33 3361):b60":c60":?b33br>a :?b33br>Author: Joseph Miller
a01]="a0":a02]="]=":a03]="q60chr33 3334):for i=1 to 3:?a01]str33 33i)aAuthor: Joseph Miller
02]q0i]q33 3358);:next i:?a03]":q60chr33 3334):for i=1 to 3:?a0
1]str33 33i)a02]q0i]q33 3358);:next i:?a03]
a60"?chr33 3397)chr33 3336)chr33 3361)chr33 3334)a33 3334)chr33 3358)a :?chr33 3397)
chr33 3336)chr33 3361)chr33 3334)a33 3334)chr33 3358)a33br>
main replaced by BEGIN.
BEGIN{c="BEGIN{c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}
S Y=(34),A="W ""S Y=(34),A=""_Y X ""F %=1:1:6 W (A,Y,%),Y,Y"" W
Y,"" X A""" X A
#!/usr/bin/perlAuthor: Christoph Durr
seek DATA, 0, 0;
print ;
__DATA__
='=%c%s%c;printf,39,,39;';printf,39,,39;
documentclass{book}pagestyle
{empty}def{nsuremath{ackslash}}def
{def{}def;{}defd{}}def
{\}
oindentg def gdef{ b
}def; c }defd{ d}def{ e
\}g} begin{document} f end
{document}}defg{ documentclass`bookd pagestyle
`emptyd def a` ensuremath` backslashdd def f
` def b` ad def c` `d def d` dd def
e` d noindent g b def b g c def b` a b
d def c` a c d def d` a dd def e` a e
d g d a begin `document d a f a end
`document dd}egin{document}nd{document}
Author: Joe Millerobeylines opagenumbers tdef[#1]#2[#3[#4
#5{#1obeylines#1nopagenumbers#1tt#1def#1[#21]#22[#23[#24
#25#3#5#4#1[#1char92]#1#2[#1char123[#1char125
#3#1[#1#21]#1#22[#1#23[#1#24
#3#1#25#4#4#1bye}[ar92]#[ar123[ar125
{[#1]#2[#3[#4
{#5}}ye
Author: Joe Miller
Languages: C and tcl
#define set char*f=Author: Joe Miller
#define F
#define proc main(){
set F "#define set char*f=
#define F
#define proc main(){
set F %c%s%c;
proc /* {} {}
puts -nonewline %cformat %cF 34 %cF 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
";
proc /* {} {}
puts -nonewline [format 34 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
t(setq /*;*/){}main(){char q='"',s='\';char*a=
"~%t(setq /*;*/){}main(){char q='~A';char*a=
~S;char*b=/*
)(setq a ~S)
(setq */ ~S;printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}~%";char*b=/*
)(setq a "\"',s='\\")
(setq */ "
t(setq /*;*/){}main(){char q='%c%c',s='%c%c';char*a=
%c%s%c;char*b=/*
)(setq a %c%c%c%c%c',s='%c%c%c%c%c)
(setq */ %c%s%c;printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}
";printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}