Quine: Difference between revisions

Content added Content deleted
(Ruby: improve wording and formatting; Rust: improve formatting)
m (fix the markup of almost all code blocks to not have extra starting/ending blank lines (only COBOL, Rust, Whitespace examples untouched as they have more complicated whitespace))
Line 365: Line 365:
=={{header|AWK}}==
=={{header|AWK}}==
===version 1===
===version 1===
<lang AWK>BEGIN{c="BEGIN{c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}</lang>
<lang AWK>
BEGIN{c="BEGIN{c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}
</lang>
===version 2===
===version 2===
<lang AWK>BEGIN{c="BEGIN{c=%c%s%c;printf c,34,c,34}";printf c,34,c,34}</lang>
<lang AWK>
BEGIN{c="BEGIN{c=%c%s%c;printf c,34,c,34}";printf c,34,c,34}
</lang>


=={{header|Babel}}==
=={{header|Babel}}==
Line 579: Line 575:


=={{header|Bob}}==
=={{header|Bob}}==
<lang bob>
<lang bob>c=","; n="\n"; q="\""; s="\\";
c=","; n="\n"; q="\""; s="\\";
v=\[
v=\[
"c=\",\"; n=\"\\n\"; q=\"\\\"\"; s=\"\\\\\";",
"c=\",\"; n=\"\\n\"; q=\"\\\"\"; s=\"\\\\\";",
Line 617: Line 612:
prtQuote(v[v.size-1]); stdout.Display(n);
prtQuote(v[v.size-1]); stdout.Display(n);
stdout.Display(v[v.size-1]); stdout.Display(n);
stdout.Display(v[v.size-1]); stdout.Display(n);
for(i=2; i<v.size-1; i++){ stdout.Display(v[i]); stdout.Display(n); }
for(i=2; i<v.size-1; i++){ stdout.Display(v[i]); stdout.Display(n); }</lang>
</lang>
Bob is a dynamic object-oriented language with syntax similar to C/C++, Java, and JavaScript. Bob was created by David Betz, a former technical editor for DDJ, and the author of XLisp and XScheme, among other languages.
Bob is a dynamic object-oriented language with syntax similar to C/C++, Java, and JavaScript. Bob was created by David Betz, a former technical editor for DDJ, and the author of XLisp and XScheme, among other languages.


Line 640: Line 634:
Using the official interpreter:
Using the official interpreter:


<lang burlesque>
<lang burlesque>blsq ) "I'm a quine."
blsq ) "I'm a quine."
"I'm a quine."</lang>
"I'm a quine."
</lang>


Every string, every number, every block is a quine.
Every string, every number, every block is a quine.
Line 692: Line 684:
cobc -x -free -frelax quine.cob
cobc -x -free -frelax quine.cob


<lang cobol>linkage section. 78 c value "display 'linkage section. 78 c value ' x'22' c x'222e20' c.". display 'linkage section. 78 c value ' x'22' c x'222e20' c.</lang>
<lang cobol>
linkage section. 78 c value "display 'linkage section. 78 c value ' x'22' c x'222e20' c.". display 'linkage section. 78 c value ' x'22' c x'222e20' c.</lang>


The following two quines were in a gray past (around 2004?) posted to the (currently inaccessible) language forum of the [http://mvshelp.net/vbforums/ mvsHelp Boards]
The following two quines were in a gray past (around 2004?) posted to the (currently inaccessible) language forum of the [http://mvshelp.net/vbforums/ mvsHelp Boards]
Line 1,248: Line 1,239:
=={{header|Erlang}}==
=={{header|Erlang}}==
<div style="width:100%;overflow:scroll">
<div style="width:100%;overflow:scroll">
<lang Erlang>
<lang Erlang>-module(quine).
-module(quine).
-export([do/0]).
-export([do/0]).


do() -> Txt=txt(), io:format("~s~ntxt() ->~n~w.~n",[Txt,Txt]), halt().
do() -> Txt=txt(), io:format("~s~ntxt() ->~n~w.~n",[Txt,Txt]), halt().
txt() ->
txt() ->
[45,109,111,100,117,108,101,40,113,117,105,110,101,41,46,10,45,101,120,112,111,114,116,40,91,100,111,47,48,93,41,46,10,10,100,111,40,41,32,45,62,32,84,120,116,61,116,120,116,40,41,44,32,105,111,58,102,111,114,109,97,116,40,34,126,115,126,110,116,120,116,40,41,32,45,62,126,110,126,119,46,126,110,34,44,91,84,120,116,44,84,120,116,93,41,44,32,104,97,108,116,40,41,46].
[45,109,111,100,117,108,101,40,113,117,105,110,101,41,46,10,45,101,120,112,111,114,116,40,91,100,111,47,48,93,41,46,10,10,100,111,40,41,32,45,62,32,84,120,116,61,116,120,116,40,41,44,32,105,111,58,102,111,114,109,97,116,40,34,126,115,126,110,116,120,116,40,41,32,45,62,126,110,126,119,46,126,110,34,44,91,84,120,116,44,84,120,116,93,41,44,32,104,97,108,116,40,41,46].</lang>

</lang>
</div>
</div>


Line 1,358: Line 1,346:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
===Código 1:===
===Código 1:===
<lang freebasic>
<lang freebasic>#Define P(X) Print X: Print "P(" & #X & ")"
#Define P(X) Print X: Print "P(" & #X & ")"
P("#Define P(X) Print X: Print ""P("" & #X & "")""")</lang>
P("#Define P(X) Print X: Print ""P("" & #X & "")""")
</lang>


===Código 2:===
===Código 2:===
<lang freebasic>Dim As String s = "Dim As String s = : Print Left(s, 18) + Chr(34) + s + Chr(34) + Mid(s, 18)" : Print Left(s, 18) + Chr(34) + s + Chr(34) + Mid(s, 18)</lang>
<lang freebasic>
Dim As String s = "Dim As String s = : Print Left(s, 18) + Chr(34) + s + Chr(34) + Mid(s, 18)" : Print Left(s, 18) + Chr(34) + s + Chr(34) + Mid(s, 18)
</lang>


=={{header|Frink}}==
=={{header|Frink}}==
Line 1,385: Line 1,369:
=={{header|Furor}}==
=={{header|Furor}}==
A very short solution, using some special features of Furor:
A very short solution, using some special features of Furor:
<lang Furor>"#s sto selfstring QUOTE @selfstring dup print QUOTE NL printnl end { „selfstring” }"
<lang Furor>
"#s sto selfstring QUOTE @selfstring dup print QUOTE NL printnl end { „selfstring” }"
#s sto selfstring QUOTE @selfstring dup print QUOTE NL printnl end { „selfstring” }</lang>
#s sto selfstring QUOTE @selfstring dup print QUOTE NL printnl end { „selfstring” }
</lang>
And behold, here is a more complex but "traditional" solution of this famous task, solved by 3 loops:
And behold, here is a more complex but "traditional" solution of this famous task, solved by 3 loops:
<lang Furor>
<lang Furor>#g §vége §eleje - tokensize / sto maxlines
#g §vége §eleje - tokensize / sto maxlines
#s
#s
7 {| {} §eleje[] printnl |}
7 {| {} §eleje[] printnl |}
Line 1,410: Line 1,391:
vége:
vége:
{ „selfstring” }
{ „selfstring” }
{ „maxlines” }
{ „maxlines” }</lang>
</lang>


=={{header|Gabuzomeu}}==
=={{header|Gabuzomeu}}==
Line 1,685: Line 1,665:


=={{header|Julia}}==
=={{header|Julia}}==
<lang Julia>
<lang Julia>x="println(\"x=\$(repr(x))\\n\$x\")"
x="println(\"x=\$(repr(x))\\n\$x\")"
println("x=$(repr(x))\n$x")</lang>
println("x=$(repr(x))\n$x")
</lang>


In Julia, <tt>$x</tt> in a string literal interpolates the value of the variable into the string.
In Julia, <tt>$x</tt> in a string literal interpolates the value of the variable into the string.
Line 1,714: Line 1,692:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<lang scheme>{{lambda {:x} :x} '{lambda {:x} :x}}
-> {lambda {:x} :x}</lang>

{{lambda {:x} :x} '{lambda {:x} :x}}
-> {lambda {:x} :x}

</lang>


=={{header|Lasso}}==
=={{header|Lasso}}==
Line 1,734: Line 1,708:


=={{header|LDPL}}==
=={{header|LDPL}}==
<lang ldpl>
<lang ldpl>DATA:
DATA:
A IS NUMBER VECTOR
A IS NUMBER VECTOR
C IS TEXT
C IS TEXT
Line 2,477: Line 2,450:
STORE 704 IN N
STORE 704 IN N


CALL SUB-PROCEDURE SHOW
CALL SUB-PROCEDURE SHOW</lang>
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
Line 2,771: Line 2,743:


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>
<lang NewLISP>(lambda (s) (print (list s (list 'quote s))))</lang>
(lambda (s) (print (list s (list 'quote s))))
</lang>


=={{header|Nim}}==
=={{header|Nim}}==
Line 2,836: Line 2,806:


=={{header|OxygenBasic}}==
=={{header|OxygenBasic}}==
<lang oxygenbasic>
<lang oxygenbasic>'RUNTIME COMPILING
'RUNTIME COMPILING


source="print source"
source="print source"


a=compile source : call a : freememory a
a=compile source : call a : freememory a</lang>
</lang>


=={{header|Oz}}==
=={{header|Oz}}==
Line 3,078: Line 3,046:
{{works with|PowerShell|2}}
{{works with|PowerShell|2}}
Adapted from Liberty BASIC.
Adapted from Liberty BASIC.
<lang PowerShell>$S = '$S = $S.Substring(0,5) + [string][char]39 + $S + [string][char]39 + [string][char]10 + $S.Substring(5)'
<lang PowerShell>
$S = '$S = $S.Substring(0,5) + [string][char]39 + $S + [string][char]39 + [string][char]10 + $S.Substring(5)'
$S.Substring(0,5) + [string][char]39 + $S + [string][char]39 + [string][char]10 + $S.Substring(5)</lang>
$S.Substring(0,5) + [string][char]39 + $S + [string][char]39 + [string][char]10 + $S.Substring(5)
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,089: Line 3,055:
<br><b>Variations to access the code directly</b><br><br>
<br><b>Variations to access the code directly</b><br><br>
<b>In a saved script</b>
<b>In a saved script</b>
<lang PowerShell>
<lang PowerShell>$MyInvocation.MyCommand.ScriptContents</lang>
$MyInvocation.MyCommand.ScriptContents
</lang>
<b>At the command line</b>
<b>At the command line</b>
<lang PowerShell>
<lang PowerShell>$MyInvocation.MyCommand.Definition</lang>
$MyInvocation.MyCommand.Definition
</lang>
<b>In a function</b>
<b>In a function</b>
<lang PowerShell>
<lang PowerShell>function Quine { $MyInvocation.MyCommand.Definition }</lang>
function Quine { $MyInvocation.MyCommand.Definition }
</lang>


=={{header|Prolog}}==
=={{header|Prolog}}==
Line 3,239: Line 3,199:
A simple and straightforward quine. Character chr(34) is a double quote, and chr(10) is a new line (Author: J. C. Lucero).
A simple and straightforward quine. Character chr(34) is a double quote, and chr(10) is a new line (Author: J. C. Lucero).
<lang python>w = "print('w = ' + chr(34) + w + chr(34) + chr(10) + w)"
<lang python>
w = "print('w = ' + chr(34) + w + chr(34) + chr(10) + w)"
print('w = ' + chr(34) + w + chr(34) + chr(10) + w)</lang>
print('w = ' + chr(34) + w + chr(34) + chr(10) + w)
</lang>


----
----
Line 3,276: Line 3,234:


make a program with this single line in it
make a program with this single line in it
<lang python>
<lang python>print(__file__[:-3])</lang>
print(__file__[:-3])
</lang>
and name the file print(__file__[:-3]).py
and name the file print(__file__[:-3]).py


Line 3,289: Line 3,245:


Python 3, from same thread at same forum, created by wewhgyih, tested by LambertDW. Works on unix and Windows7. On Windows7 I commanded python "print(__file__)" .
Python 3, from same thread at same forum, created by wewhgyih, tested by LambertDW. Works on unix and Windows7. On Windows7 I commanded python "print(__file__)" .
<lang bash>
<lang bash>$ cat print\(__file__\)
$ cat print\(__file__\)
print(__file__)
print(__file__)
$ python print\(__file__\)
$ python print\(__file__\)
print(__file__)
print(__file__)</lang>
</lang>
I saw this clever solution somewhere some time ago (dont' remember the source.) Assuming the input does not have to be a correct program...
I saw this clever solution somewhere some time ago (dont' remember the source.) Assuming the input does not have to be a correct program...
<lang>$ cat reproducing.py
<lang>$ cat reproducing.py
Line 3,312: Line 3,266:
Here's a few by me (Nathaniel Virgo). They represent attempts to make clean, readable "Pythonic" quines. The first one is straightforward, using format and chr(34) to construct a multiline string:
Here's a few by me (Nathaniel Virgo). They represent attempts to make clean, readable "Pythonic" quines. The first one is straightforward, using format and chr(34) to construct a multiline string:


<lang python>
<lang python>x = """x = {0}{1}{0}
x = """x = {0}{1}{0}
print x.format(chr(34)*3,x)"""
print x.format(chr(34)*3,x)"""
print x.format(chr(34)*3,x)
print x.format(chr(34)*3,x)</lang>
</lang>


This next one uses a base64 encoding, which is an off-the-shelf way to pack strings containing quotes and newlines etc. into ones that consist only of printable characters. Python purists will probably tell me I should have used the base64 package instead.
This next one uses a base64 encoding, which is an off-the-shelf way to pack strings containing quotes and newlines etc. into ones that consist only of printable characters. Python purists will probably tell me I should have used the base64 package instead.


<lang python>a = 'YSA9ICcnCmIgPSBhLmRlY29kZSgnYmFzZTY0JykKcHJpbnQgYls6NV0rYStiWzU6XQ=='
<lang python>
a = 'YSA9ICcnCmIgPSBhLmRlY29kZSgnYmFzZTY0JykKcHJpbnQgYls6NV0rYStiWzU6XQ=='
b = a.decode('base64')
b = a.decode('base64')
print b[:5]+a+b[5:]
print b[:5]+a+b[5:]</lang>
</lang>


In the above quine I felt it was inelegant to hard code the insertion of the data after the 5th character and I wondered if I could do it in a cleaner way. Eventually I came up with this:
In the above quine I felt it was inelegant to hard code the insertion of the data after the 5th character and I wondered if I could do it in a cleaner way. Eventually I came up with this:


<lang python>
<lang python>data = (
data = (
'ZGF0YSA9ICgKCSc=',
'ZGF0YSA9ICgKCSc=',
'JywKCSc=',
'JywKCSc=',
Line 3,335: Line 3,284:
)
)
prefix, separator, suffix = (d.decode('base64') for d in data)
prefix, separator, suffix = (d.decode('base64') for d in data)
print prefix + data[0] + separator + data[1] + separator + data[2] + suffix
print prefix + data[0] + separator + data[1] + separator + data[2] + suffix</lang>
</lang>


Finally, here's one that echoes the classic "Y combinator" way of constructing quines. First we define a function that takes some code, wraps it in a function and then applies than function to its own source code, then we apply *that* function to its own source code:
Finally, here's one that echoes the classic "Y combinator" way of constructing quines. First we define a function that takes some code, wraps it in a function and then applies than function to its own source code, then we apply *that* function to its own source code:


<lang python>
<lang python>def applyToOwnSourceCode(functionBody):
def applyToOwnSourceCode(functionBody):
print "def applyToOwnSourceCode(functionBody):"
print "def applyToOwnSourceCode(functionBody):"
print functionBody
print functionBody
print "applyToOwnSourceCode(" + repr(functionBody) + ")"
print "applyToOwnSourceCode(" + repr(functionBody) + ")"
applyToOwnSourceCode('\tprint "def applyToOwnSourceCode(functionBody):"\n\tprint functionBody\n\tprint "applyToOwnSourceCode(" + repr(functionBody) + ")"')
applyToOwnSourceCode('\tprint "def applyToOwnSourceCode(functionBody):"\n\tprint functionBody\n\tprint "applyToOwnSourceCode(" + repr(functionBody) + ")"')</lang>
</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==
Line 3,365: Line 3,311:


Another version, perhaps more understandable.
Another version, perhaps more understandable.
<lang R>src <- "\nwriteLines(c(paste(\"src <-\", encodeString(src, quote='\"')), src))"
<lang R>
src <- "\nwriteLines(c(paste(\"src <-\", encodeString(src, quote='\"')), src))"


writeLines(c(paste("src <-", encodeString(src, quote='"')), src))
writeLines(c(paste("src <-", encodeString(src, quote='"')), src))
Line 3,486: Line 3,431:
Say line
Say line
End
End
End x_
End x_</lang>
</lang>


=={{header|Ring}}==
=={{header|Ring}}==
Line 3,666: Line 3,610:


script, using printf with indexed arguments (proposed here: http://www.codecommit.com/blog/scala/useless-hackery-a-scala-quine):
script, using printf with indexed arguments (proposed here: http://www.codecommit.com/blog/scala/useless-hackery-a-scala-quine):
<lang scala>val x="val x=%c%s%1$c;printf(x,34,x)";printf(x,34,x)</lang>
<lang scala>
val x="val x=%c%s%1$c;printf(x,34,x)";printf(x,34,x)
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
Line 3,964: Line 3,906:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
Leading newline for appearance.
Leading newline for appearance.
<lang vbnet>
<lang vbnet>Module Program
Module Program
Sub Main()
Sub Main()
Dim s = "
Dim s = "
Line 4,192: Line 4,133:


For readability, here's a bash one-liner. Again, quine.sx is the filename. If you have the scat highlighter, you can pipe it to that, too. Of course, this breaks the quine, so don't compile this version expecting it to work.
For readability, here's a bash one-liner. Again, quine.sx is the filename. If you have the scat highlighter, you can pipe it to that, too. Of course, this breaks the quine, so don't compile this version expecting it to work.
<lang bash>python -c "from pprint import pprint;prog=open('quine.sx', 'r').read().split(';',24);pprint(prog)" | tr "[]" " " | perl -pe "s/( '|'(,| ))//g"</lang>
<lang bash>
python -c "from pprint import pprint;prog=open('quine.sx', 'r').read().split(';',24);pprint(prog)" | tr "[]" " " | perl -pe "s/( '|'(,| ))//g"
</lang>


=={{header|zkl}}==
=={{header|zkl}}==