Quine: Difference between revisions
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)
(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:
=={{header|AWK}}==
===version 1===
<lang AWK>BEGIN{c="BEGIN{c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}</lang>▼
▲BEGIN{c="BEGIN{c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}
===version 2===
<lang AWK>BEGIN{c="BEGIN{c=%c%s%c;printf c,34,c,34}";printf c,34,c,34}</lang>▼
▲BEGIN{c="BEGIN{c=%c%s%c;printf c,34,c,34}";printf c,34,c,34}
=={{header|Babel}}==
Line 579 ⟶ 575:
=={{header|Bob}}==
<lang bob>c=","; n="\n"; q="\""; s="\\";
v=\[
"c=\",\"; n=\"\\n\"; q=\"\\\"\"; s=\"\\\\\";",
Line 617 ⟶ 612:
prtQuote(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); }</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.
Line 640 ⟶ 634:
Using the official interpreter:
<lang burlesque>blsq ) "I'm a quine."
Every string, every number, every block is a quine.
Line 692 ⟶ 684:
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>▼
▲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]
Line 1,248 ⟶ 1,239:
=={{header|Erlang}}==
<div style="width:100%;overflow:scroll">
<lang Erlang>-module(quine).
-export([do/0]).
do() -> Txt=txt(), io:format("~s~ntxt() ->~n~w.~n",[Txt,Txt]), halt().
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].</lang>
</div>
Line 1,358 ⟶ 1,346:
=={{header|FreeBASIC}}==
===Código 1:===
<lang freebasic>#Define P(X) Print X: Print "P(" & #X & ")"
P("#Define P(X) Print X: Print ""P("" & #X & "")""")</lang>
===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>▼
▲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)
=={{header|Frink}}==
Line 1,385 ⟶ 1,369:
=={{header|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” }"▼
▲#s sto selfstring QUOTE @selfstring dup print QUOTE NL printnl end { „selfstring” }
And behold, here is a more complex but "traditional" solution of this famous task, solved by 3 loops:
<lang Furor>#g §vége §eleje - tokensize / sto maxlines
#s
7 {| {} §eleje[] printnl |}
Line 1,410 ⟶ 1,391:
vége:
{ „selfstring” }
{ „maxlines” }</lang>
=={{header|Gabuzomeu}}==
Line 1,685 ⟶ 1,665:
=={{header|Julia}}==
<lang Julia>x="println(\"x=\$(repr(x))\\n\$x\")"
In Julia, <tt>$x</tt> in a string literal interpolates the value of the variable into the string.
Line 1,714 ⟶ 1,692:
=={{header|Lambdatalk}}==
<lang scheme>{{lambda {:x} :x} '{lambda {:x} :x}}
-> {lambda {:x} :x}</lang>▼
▲-> {lambda {:x} :x}
=={{header|Lasso}}==
Line 1,734 ⟶ 1,708:
=={{header|LDPL}}==
<lang ldpl>DATA:
A IS NUMBER VECTOR
C IS TEXT
Line 2,477 ⟶ 2,450:
STORE 704 IN N
CALL SUB-PROCEDURE SHOW</lang>
=={{header|Liberty BASIC}}==
Line 2,771 ⟶ 2,743:
=={{header|NewLISP}}==
<lang NewLISP>(lambda (s) (print (list s (list 'quote s))))</lang>
=={{header|Nim}}==
Line 2,836 ⟶ 2,806:
=={{header|OxygenBasic}}==
<lang oxygenbasic>'RUNTIME COMPILING
source="print source"
a=compile source : call a : freememory a</lang>
=={{header|Oz}}==
Line 3,078 ⟶ 3,046:
{{works with|PowerShell|2}}
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)'▼
▲$S.Substring(0,5) + [string][char]39 + $S + [string][char]39 + [string][char]10 + $S.Substring(5)
{{out}}
<pre>
Line 3,089 ⟶ 3,055:
<br><b>Variations to access the code directly</b><br><br>
<b>In a saved script</b>
<lang PowerShell>$MyInvocation.MyCommand.ScriptContents</lang>
<b>At the command line</b>
<lang PowerShell>$MyInvocation.MyCommand.Definition</lang>
<b>In a function</b>
<lang PowerShell>function Quine { $MyInvocation.MyCommand.Definition }</lang>
=={{header|Prolog}}==
Line 3,239 ⟶ 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).
▲print('w = ' + chr(34) + w + chr(34) + chr(10) + w)
----
Line 3,276 ⟶ 3,234:
make a program with this single line in it
<lang python>print(__file__[:-3])</lang>
and name the file print(__file__[:-3]).py
Line 3,289 ⟶ 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__)" .
<lang bash>$ cat print\(__file__\)
print(__file__)
$ python print\(__file__\)
print(__file__)</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...
<lang>$ cat reproducing.py
Line 3,312 ⟶ 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:
<lang python>x = """x = {0}{1}{0}
print x.format(chr(34)*3,x)"""
print x.format(chr(34)*3,x)</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.
<lang python>a = 'YSA9ICcnCmIgPSBhLmRlY29kZSgnYmFzZTY0JykKcHJpbnQgYls6NV0rYStiWzU6XQ=='▼
▲a = 'YSA9ICcnCmIgPSBhLmRlY29kZSgnYmFzZTY0JykKcHJpbnQgYls6NV0rYStiWzU6XQ=='
b = a.decode('base64')
print b[:5]+a+b[5:]</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:
<lang python>data = (
'ZGF0YSA9ICgKCSc=',
'JywKCSc=',
Line 3,335 ⟶ 3,284:
)
prefix, separator, suffix = (d.decode('base64') for d in data)
print prefix + data[0] + separator + data[1] + separator + data[2] + suffix</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:
<lang python>def applyToOwnSourceCode(functionBody):
print "def applyToOwnSourceCode(functionBody):"
print functionBody
print "applyToOwnSourceCode(" + repr(functionBody) + ")"
applyToOwnSourceCode('\tprint "def applyToOwnSourceCode(functionBody):"\n\tprint functionBody\n\tprint "applyToOwnSourceCode(" + repr(functionBody) + ")"')</lang>
=={{header|Quackery}}==
Line 3,365 ⟶ 3,311:
Another version, perhaps more understandable.
<lang R>src <- "\nwriteLines(c(paste(\"src <-\", encodeString(src, quote='\"')), src))"▼
▲src <- "\nwriteLines(c(paste(\"src <-\", encodeString(src, quote='\"')), src))"
writeLines(c(paste("src <-", encodeString(src, quote='"')), src))
Line 3,486 ⟶ 3,431:
Say line
End
End x_</lang>
=={{header|Ring}}==
Line 3,666 ⟶ 3,610:
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>▼
▲val x="val x=%c%s%1$c;printf(x,34,x)";printf(x,34,x)
=={{header|Scheme}}==
Line 3,964 ⟶ 3,906:
=={{header|Visual Basic .NET}}==
Leading newline for appearance.
<lang vbnet>Module Program
Sub Main()
Dim s = "
Line 4,192 ⟶ 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.
<lang bash>python -c "from pprint import pprint;prog=open('quine.sx', 'r').read().split(';',24);pprint(prog)" | tr "[]" " " | perl -pe "s/( '|'(,| ))//g"</lang>▼
▲python -c "from pprint import pprint;prog=open('quine.sx', 'r').read().split(';',24);pprint(prog)" | tr "[]" " " | perl -pe "s/( '|'(,| ))//g"
=={{header|zkl}}==
|