| May | JUN | Jul |
| 24 | ||
| 2012 | 2013 | 2014 |
COLLECTED BY
Collection: Wide Crawl started April 2013
Quine Program
print("Hello, world.")
● So now you know how to print *anything*.
● A few things are a bit tricky, but yes -- anything.
● Ah, but can you print quotes around it ?
● It's a bit tricky, but not a problem.
print("\"Hello, world.\"")
● You've just replaced the problem of the quotes with the problem of the slash.
● No, I can print slashes as well:
print("quote: \" slash: \\ another slash: \\ another quote: \" The end.")
● Ah, but I bet you can't print the entire program.
● Sure I can.
print("print(\"Hello, world.\")")
● Yes, that prints the hello world program. But ...
● Oh, I see what you want.
print("print(\"print(\\\"Hello, world.\\\")\")")
● Yes, that prints *a* program. But it doesn't print *itself*.
● It's a bit awkward, but I suppose you want this:
print("print(\"print(\\\"print(\\\\\\\"Hello, world.\\\\\\\")\\\")\")")
● Yes, that prints *a* program. But it doesn't print *itself*.
...
● Yes, that prints *a* program. But it doesn't print *itself*.
● Oh. But that would ... that would require an infinite number of escapes.
print(" <some stuff> <infinite number of slashes >"Hello, world.<infinite number of slashes>" <more stuff> ")
● Sorry, my computer doesn't have an infinite amount of memory.
● Well then. It's impossible for a finite-size source file to hold another copy of the source file inside itself. A thing cannot be bigger than itself.
● Obviously.
A$ = "a!'dbde[39] c34);[97] 'a + c34);[98] 'b';[99] 'c';[100][101] 'e';[91] : PRINT 'CASE c';[93] ']';[33] A$;dCASE ELSE: PRINT MID$(A$, I, 1);dEND SELECTdNEXT Id" FOR I = 1 TO LEN(A$) SELECT CASE MID$(A$, I, 1) CASE CHR$(39): PRINT CHR$(34); CASE CHR$(97): PRINT "A$ = " + CHR$(34); CASE CHR$(98): PRINT "FOR I = 1 TO LEN(A$)"; CASE CHR$(99): PRINT "CHR$("; CASE CHR$(100): PRINT CASE CHR$(101): PRINT "SELECT CASE MID$(A$, I, 1)"; CASE CHR$(91): PRINT : PRINT "CASE CHR$("; CASE CHR$(93): PRINT "): PRINT"; CASE CHR$(33): PRINT A$; CASE ELSE: PRINT MID$(A$, I, 1); END SELECT NEXT I
10 LISTBut this (and any other program) that simply outputs a source code listing should be considered cheating - for a quine to be a true quine, it must surely involve quotation. [Yes, this is cheating. The canonical shortest quine using this sort of cheat was in c, with a compiler that would accept an empty file as input and construct an executable that echoed nothing to stdout. I can't recall if this was standard-conforming or just undefined behaviour though...] Almost all scripting languages allow the 'empty program' cheat. [Yeah, but it is less interesting with an interpreter - and this instance predates most scripting languages.] [It is questionable whether the 'empty program' approach even counts as cheating. It is cheating for a quine to access its own source code, either via file i/o or via special language functionality like "LIST", but an empty program does no such thing.]
:0g,:93+`#@_1+
#!/bin/sh cat $0but this is really cheating because it is using cat to read its source code. A real SRP contains a copy of its source code within the code itself.
#!/bin/catwill achieve the same function without running a shell.
b=\' c=\\ a='echo b=$c$b c=$c$c a=$b$a$b; echo $a' echo b=$c$b c=$c$c a=$b$a$b; echo $a-- Tafuni Vito - Italy - vitotafuni_AT_gmail.com
b=\' c=\\ a='echo -n b=$c$b c=$c$c a=$b$a$b\;; echo $a';echo -n b=$c$b c=$c$c a=$b$a$b\;; echo $a
b=\' c=\\ a='yes $( echo b=$c$b c=$c$c a=$b$a$b; echo $a ) | bash &' yes $( echo b=$c$b c=$c$c a=$b$a$b; echo $a ) | bash &It's a lot more heavy-duty than a traditional fork bomb, so don't complain to me if it made your system crash.
z=\' a='z=\\$z a=$z$a$z\; eval echo \$a'; eval echo $a
#include <stdio.h>
const char *data[] = {
" NULL};",
"",
"void print_string(const char *str)",
"{",
" const char *ptr;",
" printf(\" \\\"\");",
" for (ptr = str; *ptr != 0; ptr++)",
" if (*ptr == '\\\\')",
" printf(\"\\\\\\\\\");",
" else if (*ptr == '\"')",
" printf(\"\\\\\\\"\");",
" else",
" putchar(*ptr); ",
" printf(\"\\\",\\n\");",
"}",
"",
"int main(void)",
"{",
" const char **ptr;",
" printf(\"#include <stdio.h>\\n\\n\");",
" printf(\"const char *data[] = {\\n\");",
" for (ptr = data; *ptr != NULL; ptr++)",
" print_string(*ptr);",
" for (ptr = data; *ptr != NULL; ptr++)",
" printf(\"%s\\n\", *ptr);",
" return 0;",
"}",
NULL};
void print_string(const char *str)
{
const char *ptr;
printf(" \"");
for (ptr = str; *ptr != 0; ptr++)
if (*ptr == '\\')
printf("\\\\");
else if (*ptr == '"')
printf("\\\"");
else
putchar(*ptr);
printf("\",\n");
}
int main(void)
{
const char **ptr;
printf("#include <stdio.h>\n\n");
printf("const char *data[] = {\n");
for (ptr = data; *ptr != NULL; ptr++)
print_string(*ptr);
for (ptr = data; *ptr != NULL; ptr++)
printf("%s\n", *ptr);
return 0;
}
int main(void){
char str[]= " int main(void){ char str[]= %c%s%c; printf(str, 0x22, str, 0x22);}";
printf(str, 0x22, str, 0x22);}
Also see: http://www.math.uchicago.edu/~chruska/recursive/selfish.html
It contains additional examples.
((lambda (x) (quasiquote ((unquote x) (quote (unquote x))))) (quote (lambda (x) (quasiquote ((unquote x) (quote (unquote x)))))))which your pretty-printer may let you abbreviate as:
((lambda (x) `(,x ', x)) '(lambda (x) `(,x ',x)))
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
bcc32_interpreter_quine.c:
Error E2141 bcc32_interpreter_quine.c 1: Declaration syntax error
Error E2223 bcc32_interpreter_quine.c 1: Too many decimal points
*** 2 errors in Compile ***
ERROR: Parse error in file 'selected text' line 1 char 10 : ERROR: Parse error in file 'selected text' ----------------------------------- ERROR: Command line parse failed nilall parse errors seem to converge to this fixed point.
... > quine4.c:2152: error: stray ‘\342’ in program > quine4.c:2152: error: stray ‘\200’ in program > quine4.c:2152: error: stray ‘\230’ in program > quine4.c:2152: error: stray ‘\’ in program > quine4.c:2152: error: stray ‘\342’ in program > quine4.c:2152: error: stray ‘\200’ in program > quine4.c:2152: error: stray ‘\231’ in program
class Quine {
static void Main() {
string f = "class Quine {{{2} static void Main() {{{2} string f = {0}{1}{0};{2} System.Console.WriteLine?(f, (char)0x22, f, (char)10);{2} }}{2}}}";
System.Console.WriteLine?(f, (char)0x22, f, (char)10);
}
}
Let the following text, enclosed in double quotes, be called Text A. (You can really ignore Text A. Just take note of the three dollar signs it contains.) "Let the following text, enclosed in double quotes, be called Text A. (You can really ignore Text A. Just take note of the three dollar signs it contains.) $$$ Take pencil and paper, and write the following three things down, separated as paragraphs. (That's just boring copying. You needn't be interested in the text you're handling.) First, all of Text A that precedes the three dollar signs. Second, the whole of Text A, enclosed in double quotes. Third, all of Text A that follows the three dollar signs. Now step back and try to interpret what you have just written." Take pencil and paper, and write the following three things down, separated as paragraphs. (That's just boring copying. You needn't be interested in the text you're handling.) First, all of Text A that precedes the three dollar signs. Second, the whole of Text A, enclosed in double quotes. Third, all of Text A that follows the three dollar signs. Now step back and try to interpret what you have just written.Here is a variant on this idea: Change both "Now step back and try to interpret what you have just written." to "Now follow the instructions that you have just written." to make it into an infinite quine.
Object ::quine
::quine proc printSelf { } {
foreach instance [ Object info instances ] {
foreach proc [ $instance info procs ] {
puts "Object $instance"
puts "$instance proc $proc \{ [ $instance info args $proc ] \} \{
[ $instance info body $proc ]
\}"
}
}
puts "::quine printSelf"
}
::quine printSelf
Does introspection count as cheating?
: QUINE [ SOURCE ] SLITERAL TYPE ; QUINEBut if the quine is allowed to be interpreted code then you can just use this:
SOURCE TYPE
package main;import"fmt";func main(){s:="\"\\\"package main;import\"fmt\";func main(){s:=;fmt.Print(s[3:43]+s[:2]+s[:2]+s[1:2]+s[1:22]+s[1:3]+s[23:26]+s[1:3]+s[27:]+s[:1]+s[43:])}";fmt.Print(s[3:43]+s[:2]+s[:2]+s[1:2]+s[1:22]+s[1:3]+s[23:26]+s[1:3]+s[27:]+s[:1]+s[43:])}
-- by qulinxao
((++)<*>show)"((++)<*>show)"
q
public class Quine {
public static void main(String[] args) {
String[] str = {
"public class Quine {",
" public static void main(String[] args) {",
" String[] str = {",
" };",
" for(int i=0;i<3;i++)System.out.println(str[i]);",
" for(int i=0;i<9;i++)System.out.println((char)34+str[i]+(char)34+',');",
" for(int i=3;i<9;i++)System.out.println(str[i]);",
" }",
"}",
};
for(int i=0;i<3;i++)System.out.println(str[i]);
for(int i=0;i<9;i++)System.out.println((char)34+str[i]+(char)34+',');
for(int i=3;i<9;i++)System.out.println(str[i]);
}
}
--JasonWilson?
enum Q{T;System a;String b="enum Q{T;System a;String b=%c%s%c;{a.out.printf(b,34,b,34);a.exit(0);}}";{a.out.printf(b,34,b,34);a.exit(0);}}
--lf
(function a(){print('('+a+')()')})()
_='"'+";document.write('_=',_[17],_[0],_[17],'+',_,_)";document.write('_=',_[17],_[0],_[17],'+',_,_)
in Rhino maybe :
_='"'+";print('_=',_[8],_[0],_8],'+',_,_)";print('_=',_[8],_[0],_8],'+',_,_)
(,q,q,~]#~[:>:]=q=.'"_)'(,q,q,~]#~[:>:]=q=.'"_)'aaarg there should be four consecutive single quotes after the first q=. and eight after the second one but I've been unable to convince Wiki to display more than three! Could a Wiki wizard fix this, please?
s="s=%qprint(s:format(s))"print(s:format(s))http://lua-users.org/lists/lua-l/2008-05/msg00379.html
=SUBSTITUTE(SUBSTITUTE("=SUBSTITUTE(SUBSTITUTE(#@#,CHAR(35),CHAR(34)),CHAR(64),#@#)",CHAR(35),CHAR(34)),CHAR(64),"=SUBSTITUTE(SUBSTITUTE(#@#,CHAR(35),CHAR(34)),CHAR(64),#@#)")
--Jonathan Rynd
=SUBSTITUTE("=SUBSTITUTE(@,CHAR(64),CHAR(34)&@&CHAR(34))",CHAR(64),CHAR(34)&"=SUBSTITUTE(@,CHAR(64),CHAR(34)&@&CHAR(34))"&CHAR(34))
--dave@burt.id.au
@echo off %1 %2 call %0 goto e %% call %0 goto e %%3 echo.%%4 echo :f goto f :e echo.%4@echo off echo.%4%31 %32 echo.%4call %30 goto e %3%3 echo.%4call %30 goto e %3%33 echo.%3%34 echo.%4echo :f echo.%4goto f echo.%4:e :f
using System.Console;
module quine {
Main():void {
def a = @"using System.Console;
module quine {
Main():void {
def a = @;
Write(a.Substring(0,72));Write(34:>char);
Write(a);Write(34:>char);
Write(a.Substring(72,(a.Length -72)));
}
}
";
Write(a.Substring(0,72));Write(34:>char);
Write(a);Write(34:>char);
Write(a.Substring(72,(a.Length -72)));
}
}
const a=';begin write(^#^/^.^3^4^`^!^}#39,a,#39,a)end.';begin write(^#^/^.^3^4^`^!^}#39,a,#39,a)end.--Geoffrey Swift (http://www.trollied.org/~blimey/quines.php)
$_=q{print"\$_=q{$_};eval"};eval
print<<''x2,$/ print<<''x2,$/Note: both trailing newlines are necessary!
printf+qw(printf+qw(%s)x2)x2
open+0;print<0>
<?
# PHP Quine written by Ian Kjos - brooke@sf.net
$y = "function q(\$q) {
\$q = str_replace('\\\\', '\\\\\\\\', \$q);
\$q = str_replace('\$', '\\\\\$', \$q);
\$q = str_replace('\\n', '\\\\n', \$q);
\$q = str_replace('\"', '\\\\\"', \$q);
return \$q;
}
echo \"<?\\n\\n\";
echo \"# PHP Quine written by Ian Kjos - brooke@sf.net \\n\\n\";
echo '\$y = \"' . q(\$y) . '\";';
echo \"\\n\\neval(\";
echo '\$y);';
echo \"\\n\\n\\n\";
";
eval($y);
<?php
// PHP quine by Sam Barnum 360works.com
// 2003-11-08
$dna = 'PD9waHAKLy8gUEhQIHF1aW5lIGJ5IFNhbSBCYXJudW0gMzYwd29ya3MuY29tCi8vIDIwMDMtMTEtMDgKJGRuYSA9ICcqJzsKZWNobyBzdHJfcmVwbGFjZShjaHIoNDIpLCAkZG5hLCBiYXNlNjRfZGVjb2RlKCRkbmEpKTsKPz4K';
echo str_replace(chr(42), $dna, base64_decode($dna));
?>
No, it is not cheating. It is actually very good.
<? $a='chr(60).chr(63).chr(10).chr(36).chr(97).chr(61).chr(39).$a.chr(39).chr(59).chr(10)."echo $a;".chr(10).chr(63).chr(62)'; echo chr(60).chr(63).chr(10).chr(36).chr(97).chr(61).chr(39).$a.chr(39).chr(59).chr(10)."echo $a;".chr(10).chr(63).chr(62); ?>
<?
function selffunc($a) { print($a . "\n"); print("selffunc(\"" . addcslashes($a,"\n\\\"$") . "\");\n"); }
selffunc("<?\nfunction selffunc(\$a) { print(\$a . \"\\n\"); print(\"selffunc(\\\"\" . addcslashes(\$a, \"\\n\\\\\\\"\$\") . \"\\\");\\n\"); }");
function selfact($a) { print("selfact(\"" . addcslashes($a,"\n\\\"$") . "\");\n"); exec($a); }
selffunc("function selfact(\$a) { print(\"selfact(\\\"\" . addcslashes(\$a,\"\\n\\\\\\\"\$\") . \"\\\");\\n\"); exec(\$a); }");
function selffunc2($a) { print("selffunc2(\"" . addcslashes($a,"\\\"$") . "\");\n"); print($a . "\n"); }
selffunc("function selffunc2(\$a) { print(\"selffunc2(\\\"\" . addcslashes(\$a,\"\\\\\\\"\$\") . \"\\\");\\n\"); print(\$a . \"\\n\"); }");
//The Self Act Code executes any command.
selffunc("//The Self Act Code executes any command.");
selfact("\$a = fopen('log.txt','w'); fwrite(\$a,\"Hello\"); fclose(\$a);");
selffunc2("?>");
?>
<?printf($c='<?printf($c=%s,\'\\\'\'.addslashes($c).\'\\\'\');','\.addslashes($c).'\);I wish this wiki had a way to add literal characters so that I could directly paste it over, but it won't let me.
<?$a='<?$a=%c%s%c;printf($a,39,$a,39);';printf($a,39,$a,39);(Actually, the 8 bytes longer
<?php $a='<?php $a=%c%s%c;printf($a,39,$a,39);';printf($a,39,$a,39);is the preferred form, since the short '<?' may not be supported on all servers) On the web I found the even shorter PHP quine, written by Trevor Sayre:
<?php printf($a='<?php printf($a=%c%s%c,39,$a,39);',39,$a,39);or
<?printf($a='<?printf($a=%c%s%c,39,$a,39);',39,$a,39);-- Tom van der Beek
<?printf(~$s=ÃÀ–‘‹™×ÛŒÂÚŒÓÛŒÖÄ,$s);This will need to be saved with ANSI character encoding to work properly. Or alternatively, it may be generated with the following:
<?printf(~$s="\xc3\xc0\x8f\x8d\x96\x91\x8b\x99\xd7\x81\xdb\x8c\xc2\xda\x8c\xd3\xdb\x8c\xd6\xc4",$s);-- Mike Tryczak (a.k.a. primo)
<?
function c($b) { return "<"."?\n$b?".">\n$b"; } ob_start("c");
?>
function c($b) { return "<"."?\n$b?".">\n$b"; } ob_start("c");
<?php $sFile = file_get_contents(__FILE__); echo $sFile;
<?=file_get_contents(__FILE__);or alternatively:
<?readfile(__FILE__);
%dcl z%z='put edit';proc options(main;q=''''put list(m;do i=1,2;z(q)skip;do j= 1to 78c=substr(m(i),j;if c=q z(c;z(c;end;z(q',';dcl(c,q)char,m(2)char(99)init( '%dcl z%z=''put edit'';proc options(main;q=''''''''put list(m;do i=1,2;z(q)skip;do j=', '1to 78c=substr(m(i),j;if c=q z(c;z(c;end;z(q'','';dcl(c,q)char,m(2)char(99)init(',This entry has big trouble with repeated single quotes...
#include <stdio.h> #define q(a,...) a #define substr q #define eval(a) main(){char c[]=a,n=10;c[419]=0;printf(c+4,n,n,n,n,34,34,n,34,39,c+4,39,34,n); }/* Copyright (C) Thomas Schumm <phong@phong.org> exec("from sys import*;substr=q=lambda y:exit(stdout.write(y[4:-46]%((10,)*4+(34,34,10,34,39, y[4:-46],39,34,10))))",None);#*/ eval(substr(q("$p='#include <stdio.h>%c#define q(a,...) a%c#define substr q%c#define eval(a) main() {char c[]=a,n=10;c[419]=0;printf(c+4,n,n,n,n,34,34,n,34,39,c+4,39,34,n); }/* Copyright (C) Thomas Schumm <phong@phong.org>%cexec(%cfrom sys import*;substr=q=lambda y: exit(stdout.write(y[4:-46]%%((10,)*4+(34,34,10,34,39,y[4:-46],39,34,10))))%c,None);# */%ceval(substr(q(%c$p=%c%s%c;printf($p,(10)x4,34,34,10,34,39,$p,39,34,10)%c),1,-1))%c'; printf($p,(10)x4,34,34,10,34,39,$p,39,34,10)"),1,-1))When I compiled and ran as C/C++, it didn't work (missing only a few characters)--a change made to gcc? Working fine for me with gcc 3.3.4... The original is here (I may occasionally update it if it gets better or smaller): http://www.phong.org/bf/polyglotC++PerlPythonC.c -- TomSchumm
;; (*.) = {- *) let (@@) x y = x::y let e = [] let a = (*
(letrec ((a '(
; -} -- *)
" " @@
" A polyglot quine in " @@
" Haskell & O'Caml & Scheme " @@
" " @@
" Usage: runhugs thisfile # www.haskell.org/hugs " @@
" ocamlc -o x thisfile.ml ;./x # www.ocaml.org " @@
" scsh -s thisfile # www.scsh.net " @@
" " @@
"" @@
";; (*.) = {- *) let (@@) x y = x::y let e = [] let a = (*" @@
"(letrec ((a '(" @@
"; -} -- *)" @@
"" @@
" e" @@
";; (*:) = [\" \" ++ show x ++ \" @@\" | x<-( *.)]; main = {-" @@
"; -} mapM_ putStrLn (x ++ ( *:) ++ y); (x, _:y) = {-" @@
"; -} span p (tail (dropWhile p ( *.))); p = (/= \"\"); infixr {-" @@
"; -} @@; (@@) = (:); e = [] {- *) let rec s = function [] -> (*" @@
"; *) [],[] | \"\"::y -> [],y | x::y -> let a,b = s y (*" @@
"; *) in x::a,b let b,d = s (snd (s a)) let f = String.escaped (*" @@
"; *) let c = List.map (fun x -> \" \\\"\" ^ f x ^ \"\\\" @@\") a" @@
";; List.iter print_endline (b @ c @ d) (*" @@
")) (f (lambda (x) (if (null? x) x (if (string? (car x)) (cons (" @@
"car x) (f (cdr x))) (f (cdr x)))))) (g (lambda (x) (if (string=?" @@
"\"\" (car x)) (cons '() (cdr x)) (let ((y (g (cdr x)))) (cons (" @@
"cons (car x) (car y)) (cdr y)))))) (h (lambda (x) (if (null? x)" @@
"#f (begin (display (car x)) (newline) (h (cdr x)))))) (i (lambda" @@
"(x) (if (null? x) #f (begin (display \" \") (write (car x)) (" @@
"display \" @@\") (newline) (i (cdr x))))))) (let ((b (g (cdr (g" @@
"(f a)))))) (h (car b)) (i (f a)) (h (cdr b))))" @@
"; -} -- *)" @@
e
;; (*:) = [" " ++ show x ++ " @@" | x<-( *.)]; main = {-
; -} mapM_ putStrLn (x ++ ( *:) ++ y); (x, _:y) = {-
; -} span p (tail (dropWhile p ( *.))); p = (/= ""); infixr {-
; -} @@; (@@) = (:); e = [] {- *) let rec s = function [] -> (*
; *) [],[] | ""::y -> [],y | x::y -> let a,b = s y (*
; *) in x::a,b let b,d = s (snd (s a)) let f = String.escaped (*
; *) let c = List.map (fun x -> " \"" ^ f x ^ "\" @@") a
;; List.iter print_endline (b @ c @ d) (*
)) (f (lambda (x) (if (null? x) x (if (string? (car x)) (cons (
car x) (f (cdr x))) (f (cdr x)))))) (g (lambda (x) (if (string=?
"" (car x)) (cons '() (cdr x)) (let ((y (g (cdr x)))) (cons (
cons (car x) (car y)) (cdr y)))))) (h (lambda (x) (if (null? x)
#f (begin (display (car x)) (newline) (h (cdr x)))))) (i (lambda
(x) (if (null? x) #f (begin (display " ") (write (car x)) (
display " @@") (newline) (i (cdr x))))))) (let ((b (g (cdr (g
(f a)))))) (h (car b)) (i (f a)) (h (cdr b))))
; -} -- *)
D'oh, that's got me beat. -- TomSchumm
(dup == =) dup == =(note this doesn't produce output to paper, but rather to the terminal; run it with gs) --Chris King
sourceline 1That's cheating, though, akin to the bash one above that uses cat $0
puts(s = <<e, s, 'e') puts(s = <<e, s, 'e') e
s="s=%s;printf s,s.dump";printf s,s.dump
((lambda (x) `(,x ',x)) '(lambda (x) `(,x ',x)))Using outside-language facilities to access the source is considered cheating. E.g. opening the source as a text file would be cheating. But cheating in original ways is strongly encouraged. ;-)
{ thisFunction.asCompileString ++ ".value" }.value
repeat x 2 : output xpose list 2 crlf <<" repeat x 2 : output xpose list 2 crlf <<"
<*For a self-reproducing program in Oberon, see http://www.modulaware.com/mdlt/mdlt78.htm For a lot more see http://www.nyx.net/~gthompso/quine.htm
EditText of this page
(last edited June 22, 2013)
or FindPage with title or text search