Quine Generating Programs It is possible (and actually in some cases easier) to write a program which outputs another program which is itself a quine. Language: BrainF*** Author: Bertram Felgenhauer Notes: this is a bootstrap, use with itself as input. The quine can easily be reduced by 5 characters, but it wwould be much slower. This is left as an excercise to the reader. The bootstrap requires a 16-bits brainf***; the resulting quine works on a 8-bit brainf***, too. + [,>++++++++[<-------->-]<] ++++[>++++<-]>[>++++<-]>[>++++<-]>[-[>+<-]>] + [>+<43-------------------------------------------- [>+<45-- [>+<46- [>+<60-------------- [>+<62-- [>+<91----------------------------- [>+<93-- [>[-]]]]]]]]<,+]>>[>]< @ [<]<+++++++[>+++++++++<-]>-..> [<.<+++++[>----<-]>+>[<<+>.>-]<[>+<-]>+++++++++++++++++++>] <<< [ -[>++<-[>+<-[>++++++++++++++<-[>++<-[>+<+++++++++++++[>++<-]]]]]] ++++++[>+++++++<-]>+.<< ] Author: Eli Barzilay (eli@CS.Cornell.EDU) Note: JavaScript(Scheme-style) (function (x) { return unescape(x)+"("+"\""+x+"\""+")"}) (escape("(function (x) { return unescape(x)+\"(\"+\"\\\"\"+x+\"\\\"\"+\")\"})")) Author: Tanaka Tomoyuki Note: After two iterations you get a quine ((lambda (c) `((lambda (c) ,c) ',c)) '`((lambda (c) (if (procedure? c) (c ',c) ,c)) (call/cc call/cc))) Author: Louise Hay Note: call the bootstrap function B. cset the atom B to the value of GET(B EXPR) and then evaluate (B B). This result in a quine (LAMBDA (Y) (LIST (LIST (QUOTE LAMBDA) NIL (LIST Y (LIST (QUOTE QUOTE) Y))))) Author: Fred Galvin (galvin@math.ukans.edu) Note: BASIC 10 X$="+CHR$(34):PRINT MID$(X$,35)+X$+X$'1 X$=" 20 X$=X$+CHR$(34) 30 PRINT MID$(X$,35)+X$+X$ Author: Louise Hay Note: BASIC 10 REM THIS IS A BOOTSTRAP 20 DIM P$(800),X$(58) 30 N=13:L=58 40 S$=CHR$(32):B$=CHR$(80):D$=CHR$(36) 50 E$=CHR$(61):A$=CHR$(43):Q$=CHR$(34) 60 FOR I=1 TO N 70 READ X$ 80 P$=P$+X$ 90 NEXT I 100 PRINT 10,S$+P$(I,L) 110 PRINT 20,S$+P$(L+1,2*L) 120 FOR I=1 TO N 130 PRINT 20+10*I,S$+B$+D$+E$+B$+D$+A$+Q$+P$(L*(I-1)+1,L*I)+Q$ 140 NEXT I 150 FOR I = 3 TO N 160 PRINT (N+1)*10,S$+P$(L*(I-1)+1,L*I) 170 NEXT I 180 STOP 190 REM THIS IS A SELF REPRODUCING PROGRAM 200 DIM P$(800) 210 DATA "S$=CHR$(32):B$=CHR$(80):D$=CHR$(36) " 220 DATA "E$=CHR$(61):A$=CHR$(43):Q$=CHR$(34) " 230 DATA "N=13:L=50 " 240 DATA "PRINT 10;S$+P$(1,L) " 250 DATA "PRINT 20;S$+P$(L+1,2*L) " 260 DATA "FOR I=1 TO N " 270 DATA "PRINT 20+10*I;S$+B$+D$+F$+P$+D$+A$+Q$+P$(L*(I-1)+1,L*I)+Q$" 280 DATA "NEXT I " 290 DATA "FOR I=3 TO N " 300 DATA "PRINT (N+I)*10;S$+P$(L*(I-1)+1,L*I) " 310 DATA "NEXT I " Author: Ken Thompson, inventor of UNIX Note: C (like he'd give an example in anything else) char s[] = { '\t', '0', '\n', '}', ';', '\n', '\n', 'm', 'a', 'i', 'n', '(', ')', '\n', '{', '\n', '\t', 'i', 'n', 't', ' ', 'i', ';', '\n', '\n', '\t', 'p', 'r', 'i', 'n', 't', 'f', '(', '\"', 'c', 'h', 'a', 'r', ' ', '\\', 't', 's', '[', ']', ' ', '=', ' ', '{', '\\', 'n', '\"', ')', ';', '\n', '\t', 'f', 'o', 'r', '(', 'i', '=', '0', ';', 's', '[', 'i', ']', ';', 'i', '+', '+', ')', '\n', '\t', '\t', 'p', 'r', 'i', 'n', 't', 'f', '(', '\"', '\\', 'r', '%', 'd', ',', '\\', 'n', '\"', ',', 's', '[', 'i', ']', ')', ';', '\n', '\t', 'p', 'r', 'i', 'n', 't', 'f', '(', '\"', '%', 's', '\"', ',', 's', ')', ';', '\n', '}', 0 }; main() { int i; printf("char \ts[] = {\n"); for(i=0;s[i];i++) printf("\r%d,\n",s[i]); printf("%s",s); }