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.edu
main(){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>
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);}\n";printf(c+2,c[10
2],c[1],*c,*c,c,*c,c[1]);exit(0);}
Author: Joe Miller
#define T(a) main(){printf(a,#a);}
T("#define T(a) main(){printf(a,#a);}\nT(%s)")
Author: Dan Hoey
/**/char q='"',*a="*//**/char q='%c',*a=%c%s%c*/};)b(stup;]d[b=]d-852
[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/**/
Author: smrPretty 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 $x x; set x}; regsub @ $x $x 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 'QU OTE X))))Author: Joe Miller
((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
a$=chr$(34):d$=chr$(58)+chr$(99)+chr$(36)+chr$(61):b$="a$=chr$(34):d$ =chr$(58)+chr$(99)+chr$(36)+chr$(61):b$=":c$=":?b$ a$ b$ a$ d$ a$ c$ a$ c$":?b$ a$ b$ a$ d$ a$ c$ a$ c$Author: Joseph Miller
a$[1]="a$[":a$[2]="]=":a$[3]="q$=chr$(34):for i=1 to 3:?a$[1]str$(i)a $[2]q$ a$[i]q$ chr$(58);:next i:?a$[3]":q$=chr$(34):for i=1 to 3:?a$[ 1]str$(i)a$[2]q$ a$[i]q$ chr$(58);:next i:?a$[3]Author: Joseph Miller
a$="?chr$(97)chr$(36)chr$(61)chr$(34)a$ chr$(34)chr$(58)a$":?chr$(97) chr$(36)chr$(61)chr$(34)a$ chr$(34)chr$(58)a$
main replaced by BEGIN.
BEGIN{c="BEGIN{c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}
S Y=$C(34),A="W ""S Y=$C(34),A=""_Y X ""F %=1:1:6 W $P(A,Y,%),Y,Y"" W Y,"" X A""" X A
#!/usr/bin/perl seek DATA, 0, 0; print ; __DATA__Author: Christoph Durr
$b='$b=%c%s%c;printf$b,39,$b,39;';printf$b,39,$b,39;
\documentclass{book}\pagestyle
{empty}\def\a{\ensuremath{\backslash}}\def\f
{\def\b{\a}\def\c{\{}\def\d{\}}\def
\e{\\}\noindent\g\b def\b g\c\def\b{\a b
}\def\c{\a c }\def\d{\a d}\def\e{\a e
\\}\g\}\a begin\{document\}\a f\a end
\{document\}}\def\g{ \b documentclass\c book\d\b pagestyle\e
\c empty\d\b def\b a\c \b ensuremath\c \b backslash\d\d\b def\b f\e
\c \b def\b b\c \b a\d\b def\b c\c \b \c \d\b def\b d\c \b \d\d\b def\e
\b e\c \b \b \d\b noindent\b g\b b def\b b g\b c\b def\b b\c \b a b\e
\d\b def\b c\c \b a c \d\b def\b d\c \b a d\d\b def\b e\c \b a e\e
\b \b \d\b g\b \d\b a begin\b \c document\b \d\b a f\b a end\e
\b \c document\b \d\d}\begin{document}\f\end{document}
Author: Joe Miller
\obeylines\nopagenumbers\tt\def\[#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}\[\char92]\#[\char123[\char125
{\[\#1]\#2[\#3[\#4
{\#5}}\bye
Author: Joe Miller
Languages: C and tcl
#define set char*f=
#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 $F 34 $F 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
Author: Joe Miller
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 */);}
|
|