First commit of partial RosettaCode contents.
Pushing this for testing purposes. Lots of work still needed.
This commit is contained in:
commit
1e05ecd7ee
781 changed files with 9080 additions and 0 deletions
17
Task/Quine/0DESCRIPTION
Normal file
17
Task/Quine/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
A [[wp:Quine_%28computing%29|Quine]] is a self-referential program that can, without any external access, output its own source. It is named after the [[wp:Willard_Van_Orman_Quine|philosopher and logician]] who studied self-reference and quoting in natural language, as for example in the paradox "'Yields falsehood when preceded by its quotation' yields falsehood when preceded by its quotation."
|
||||
|
||||
"Source" has one of two meanings. It can refer to the text-based program source. For languages in which program source is represented as a data structure, "source" may refer to the data structure: quines in these languages fall into two categories: programs which print a textual representation of themselves, or expressions which evaluate to a data structure which is equivalent to that expression.
|
||||
|
||||
The usual way to code a Quine works similarly to this paradox: The program consists of two identical parts, once as plain code and once ''quoted'' in some way (for example, as a character string, or a literal data structure). The plain code then accesses the quoted code and prints it out twice, once unquoted and once with the proper quotation marks added. Often, the plain code and the quoted code have to be nested.
|
||||
|
||||
Write a program that outputs its own source code in this way. If the language allows it, you may add a variant that accesses the code directly. You are not allowed to read any external files with the source code. The program should also contain some sort of self-reference, so constant expressions which return their own value which some top-level interpreter will print out. Empty programs producing no output are not allowed.
|
||||
|
||||
There are several difficulties that one runs into when writing a quine, mostly dealing with quoting:
|
||||
* Part of the code usually needs to be stored as a string or structural literal in the language, which needs to be quoted somehow. However, including quotation marks in the string literal itself would be troublesome because it requires them to be escaped, which then necessitates the escaping character (e.g. a backslash) in the string, which itself usually needs to be escaped, and so on.
|
||||
** Some languages have a function for getting the "source code representation" of a string (i.e. adds quotation marks, etc.); in these languages, this can be used to circumvent the quoting problem.
|
||||
** Another solution is to construct the quote character from its [[character code]], without having to write the quote character itself. Then the character is inserted into the string at the appropriate places. The ASCII code for double-quote is 34, and for single-quote is 39.
|
||||
* Newlines in the program may have to be reproduced as newlines in the string, which usually requires some kind of escape sequence (e.g. "\n"). This causes the same problem as above, where the escaping character needs to itself be escaped, etc.
|
||||
** If the language has a way of getting the "source code representation", it usually handles the escaping of characters, so this is not a problem.
|
||||
** Some languages allow you to have a string literal that spans multiple lines, which embeds the newlines into the string without escaping.
|
||||
** Write the entire program on one line, for free-form languages (as you can see for some of the solutions here, they run off the edge of the screen), thus removing the need for newlines. However, this may be unacceptable as some languages require a newline at the end of the file; and otherwise it is still generally good style to have a newline at the end of a file. (The task is not clear on whether a newline is required at the end of the file.) Some languages have a print statement that appends a newline; which solves the newline-at-the-end issue; but others do not.
|
||||
<br>See the nostalgia note under Fortran.
|
||||
25
Task/Quine/BASIC/quine-2.bas
Normal file
25
Task/Quine/BASIC/quine-2.bas
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
READ d$
|
||||
DO
|
||||
READ x$
|
||||
PRINT x$
|
||||
LOOP UNTIL LEN(x$) < 1
|
||||
RESTORE
|
||||
DO
|
||||
READ x$
|
||||
PRINT d$; CHR$(34); x$; CHR$(34)
|
||||
LOOP UNTIL LEN(x$) < 1
|
||||
END
|
||||
|
||||
DATA "DATA "
|
||||
DATA "READ d$"
|
||||
DATA "DO"
|
||||
DATA " READ x$"
|
||||
DATA " PRINT x$"
|
||||
DATA "LOOP UNTIL LEN(x$) < 1"
|
||||
DATA "RESTORE"
|
||||
DATA "DO"
|
||||
DATA " READ x$"
|
||||
DATA " PRINT d$; CHR$(34); x$; CHR$(34)"
|
||||
DATA "LOOP UNTIL LEN(x$) < 1"
|
||||
DATA "END"
|
||||
DATA ""
|
||||
1
Task/Quine/BASIC/quine.bas
Normal file
1
Task/Quine/BASIC/quine.bas
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 LIST
|
||||
1
Task/Quine/Befunge/quine.bf
Normal file
1
Task/Quine/Befunge/quine.bf
Normal file
|
|
@ -0,0 +1 @@
|
|||
:0g,:66+`#@_1+
|
||||
10
Task/Quine/C/quine.c
Normal file
10
Task/Quine/C/quine.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
|
||||
static char sym[] = "\n\t\\\"";
|
||||
|
||||
int main(void) {
|
||||
const char *code = "#include <stdio.h>%c%cstatic char sym[] = %c%cn%ct%c%c%c%c%c;%c%cint main(void) {%c%cconst char *code = %c%s%c;%c%cprintf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);%c%c%creturn 0;%c}%c";
|
||||
printf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
Task/Quine/Clojure/quine.clj
Normal file
1
Task/Quine/Clojure/quine.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
((fn [x] (list x (list (quote quote) x))) (quote (fn [x] (list x (list (quote quote) x)))))
|
||||
1
Task/Quine/CoffeeScript/quine.coffee
Normal file
1
Task/Quine/CoffeeScript/quine.coffee
Normal file
|
|
@ -0,0 +1 @@
|
|||
s="s=#&# ;alert s.replace(/&/,s).replace /#(?=[^&;'(]|';;$)/g, '#';;" ;alert s.replace(/&/,s).replace /#(?=[^&;'(]|';;$)/g, '"';;
|
||||
6
Task/Quine/Erlang/quine.erl
Normal file
6
Task/Quine/Erlang/quine.erl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
-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].
|
||||
1
Task/Quine/Forth/quine.fth
Normal file
1
Task/Quine/Forth/quine.fth
Normal file
|
|
@ -0,0 +1 @@
|
|||
SOURCE TYPE
|
||||
1
Task/Quine/Fortran/quine.f
Normal file
1
Task/Quine/Fortran/quine.f
Normal file
|
|
@ -0,0 +1 @@
|
|||
character*46::s='("character*46::s=",3a,";print s,39,s,39;end")';print s,39,s,39;end
|
||||
8
Task/Quine/Go/quine.go
Normal file
8
Task/Quine/Go/quine.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\ta := %q\n\tfmt.Printf(a, a)\n}\n"
|
||||
fmt.Printf(a, a)
|
||||
}
|
||||
1
Task/Quine/Haskell/quine-2.hs
Normal file
1
Task/Quine/Haskell/quine-2.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
(\s -> putStrLn (s ++ show s)) "(\\s -> putStrLn (s ++ show s)) "
|
||||
1
Task/Quine/Haskell/quine-3.hs
Normal file
1
Task/Quine/Haskell/quine-3.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
(putStrLn . \s -> s ++ show s) "(putStrLn . \\s -> s ++ show s) "
|
||||
2
Task/Quine/Haskell/quine-4.hs
Normal file
2
Task/Quine/Haskell/quine-4.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import Control.Monad.Reader
|
||||
(putStrLn . ap (++) show) "(putStrLn . ap (++) show) "
|
||||
1
Task/Quine/Haskell/quine-5.hs
Normal file
1
Task/Quine/Haskell/quine-5.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
ap(++)show"ap(++)show"
|
||||
1
Task/Quine/Haskell/quine.hs
Normal file
1
Task/Quine/Haskell/quine.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
let q s = putStrLn (s ++ show s) in q "let q s = putStrLn (s ++ show s) in q "
|
||||
1
Task/Quine/Java/quine-2.java
Normal file
1
Task/Quine/Java/quine-2.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
class S{public static void main(String[]a){String p="class S{public static void main(String[]a){String p=%c%s%1$c;System.out.printf(p,34,p);}}";System.out.printf(p,34,p);}}
|
||||
1
Task/Quine/Java/quine.java
Normal file
1
Task/Quine/Java/quine.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
class S{public static void main(String[]a){String s="class S{public static void main(String[]a){String s=;char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}";char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}
|
||||
1
Task/Quine/JavaScript/quine-2.js
Normal file
1
Task/Quine/JavaScript/quine-2.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
var code='var q=String.fromCharCode(39);print("var code="+q+code+q+";eval(code)")';eval(code)
|
||||
1
Task/Quine/JavaScript/quine.js
Normal file
1
Task/Quine/JavaScript/quine.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
(function(){print("("+arguments.callee.toString().replace(/\s/g,'')+")()");})()
|
||||
1
Task/Quine/Lua/quine.lua
Normal file
1
Task/Quine/Lua/quine.lua
Normal file
|
|
@ -0,0 +1 @@
|
|||
s=[[io.write('s=[','[',s,']','];',s)]];io.write('s=[','[',s,']','];',s)
|
||||
2
Task/Quine/PHP/quine.php
Normal file
2
Task/Quine/PHP/quine.php
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
|
||||
'; printf($p,39,$p,39); ?>
|
||||
3
Task/Quine/Perl/quine-2.pl
Normal file
3
Task/Quine/Perl/quine-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
seek(DATA, 0, 0);
|
||||
print <DATA>
|
||||
__DATA__
|
||||
1
Task/Quine/Perl/quine-3.pl
Normal file
1
Task/Quine/Perl/quine-3.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
open ME, $0 and print <ME>;
|
||||
1
Task/Quine/Perl/quine-4.pl
Normal file
1
Task/Quine/Perl/quine-4.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
$_=q{print"\$_=q{$_};eval"};eval
|
||||
47
Task/Quine/Perl/quine-5.pl
Normal file
47
Task/Quine/Perl/quine-5.pl
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
{local$_=q{
|
||||
{
|
||||
package Quine;
|
||||
use strict;
|
||||
use warnings;
|
||||
our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g;
|
||||
my $head = '{local$_=q' . "\x7B\n";
|
||||
my $tail = 'print"{local\$_=q{$_};eval}\n"' . "\x7D;eval}\n";
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $quine = $head . shift;
|
||||
my $ret = shift || 1;
|
||||
my $ln = ( $quine =~ tr/\n/\n/ );
|
||||
$ln++;
|
||||
$quine .= "return $ret if caller(1)or(caller)[2]!=$ln;$tail";
|
||||
bless \$quine, $class;
|
||||
}
|
||||
|
||||
sub from_file {
|
||||
my ( $class, $fn, $ret ) = @_;
|
||||
local $/;
|
||||
open my $fh, '<', $fn or die "$fn : $!";
|
||||
my $src = <$fh>;
|
||||
close $fh;
|
||||
$class->new( $src, $ret );
|
||||
}
|
||||
|
||||
sub quine { ${ $_[0] } }
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Quine - turn your perl modules/apps into a true quine!
|
||||
|
||||
=head1 VERSION
|
||||
|
||||
$Id: Quine.pm,v 0.2 2010/09/15 20:23:53 dankogai Exp dankogai $
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use Quine;
|
||||
print Quine->from_file("woot.pl")->quine;
|
||||
print Quine->from_file("moot.psgi", '$app')->quine;
|
||||
|
||||
=cut
|
||||
}
|
||||
return 1 if caller(1);print"{local\$_=q{$_};eval}\n"};eval}
|
||||
2
Task/Quine/Perl/quine.pl
Normal file
2
Task/Quine/Perl/quine.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$s = q($s = q(%s); printf($s, $s);
|
||||
); printf($s, $s);
|
||||
1
Task/Quine/PicoLisp/quine-2.l
Normal file
1
Task/Quine/PicoLisp/quine-2.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(let X '(list 'let 'X (lit X) X) (list 'let 'X (lit X) X))
|
||||
1
Task/Quine/PicoLisp/quine.l
Normal file
1
Task/Quine/PicoLisp/quine.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
('((X) (list (lit X) (lit X))) '((X) (list (lit X) (lit X))))
|
||||
1
Task/Quine/Python/quine-2.py
Normal file
1
Task/Quine/Python/quine-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
x = 'x = {!r};print(x.format(x))';print(x.format(x))
|
||||
1
Task/Quine/Python/quine-3.py
Normal file
1
Task/Quine/Python/quine-3.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
import sys; sys.stdout.write(open(sys.argv[0]).read())
|
||||
1
Task/Quine/Python/quine-4.py
Normal file
1
Task/Quine/Python/quine-4.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
print(__file__[:-3])
|
||||
2
Task/Quine/Python/quine-5.py
Normal file
2
Task/Quine/Python/quine-5.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$ python print\(__file__\[\:-3\]\).py
|
||||
print(__file__[:-3])
|
||||
2
Task/Quine/Python/quine.py
Normal file
2
Task/Quine/Python/quine.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
x = 'x = %r\nprint(x %% x)'
|
||||
print(x % x)
|
||||
4
Task/Quine/REXX/quine-2.rexx
Normal file
4
Task/Quine/REXX/quine-2.rexx
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/*REXX program outputs its own source including comments and blank lines.*/
|
||||
do k=1 for sourceline()
|
||||
say sourceline(k)
|
||||
end /*k*/
|
||||
52
Task/Quine/REXX/quine-3.rexx
Normal file
52
Task/Quine/REXX/quine-3.rexx
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* Rexx */
|
||||
|
||||
Q = "'"
|
||||
Queue '/* Rexx */'
|
||||
Queue ''
|
||||
Queue 'Q = "$"'
|
||||
Queue '&QQQ'
|
||||
Queue ''
|
||||
Queue 'X = 0'
|
||||
Queue 'Do while Queued() \= 0'
|
||||
Queue ' Parse pull code'
|
||||
Queue ' X = X + 1; codel.0 = X; codel.X = code'
|
||||
Queue ' End'
|
||||
Queue ''
|
||||
Queue 'Do x_ = 1 for codel.0'
|
||||
Queue ' line = codel.x_'
|
||||
Queue ' If abbrev(line, "Q = ") then Do'
|
||||
Queue ' line = translate(line, Q, "$")'
|
||||
Queue ' End'
|
||||
Queue ' If line = "&QQQ" then Do'
|
||||
Queue ' Do y_ = 1 to codel.0'
|
||||
Queue ' qline = codel.y_'
|
||||
Queue ' Say "Queue" Q || qline || Q'
|
||||
Queue ' End y_'
|
||||
Queue ' End'
|
||||
Queue ' else Do'
|
||||
Queue ' Say line'
|
||||
Queue ' End'
|
||||
Queue ' End x_'
|
||||
Queue ''
|
||||
|
||||
X = 0
|
||||
Do while Queued() \= 0
|
||||
Parse pull code
|
||||
X = X + 1; codel.0 = X; codel.X = code
|
||||
End
|
||||
|
||||
Do x_ = 1 for codel.0
|
||||
line = codel.x_
|
||||
If abbrev(line, "Q = ") then Do
|
||||
line = translate(line, Q, "$")
|
||||
End
|
||||
If line = "&QQQ" then Do
|
||||
Do y_ = 1 to codel.0
|
||||
qline = codel.y_
|
||||
Say "Queue" Q || qline || Q
|
||||
End y_
|
||||
End
|
||||
else Do
|
||||
Say line
|
||||
End
|
||||
End x_
|
||||
1
Task/Quine/REXX/quine.rexx
Normal file
1
Task/Quine/REXX/quine.rexx
Normal file
|
|
@ -0,0 +1 @@
|
|||
/*REXX program outputs itself.*/ do j=1 for sourceline(); say sourceline(j); end
|
||||
3
Task/Quine/Ruby/quine-2.rb
Normal file
3
Task/Quine/Ruby/quine-2.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
puts <<e*2,'e'
|
||||
puts <<e*2,'e'
|
||||
e
|
||||
1
Task/Quine/Ruby/quine-3.rb
Normal file
1
Task/Quine/Ruby/quine-3.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
eval s="puts'eval s='+s.inspect"
|
||||
1
Task/Quine/Ruby/quine.rb
Normal file
1
Task/Quine/Ruby/quine.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
x = "x = %p; puts x %% x"; puts x % x
|
||||
13
Task/Quine/Scala/quine-2.scala
Normal file
13
Task/Quine/Scala/quine-2.scala
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
object Quine {
|
||||
def main(args: Array[String]) {
|
||||
val q = "\"" * 3
|
||||
val c = """object Quine {
|
||||
def main(args: Array[String]) {
|
||||
val q = "\"" * 3
|
||||
val c = %s%s%s
|
||||
println(c format (q, c, q))
|
||||
}
|
||||
}"""
|
||||
println(c format (q, c, q))
|
||||
}
|
||||
}
|
||||
6
Task/Quine/Scala/quine.scala
Normal file
6
Task/Quine/Scala/quine.scala
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
val q = "\"" * 3
|
||||
val c = """val q = "\"" * 3
|
||||
val c = %s%s%s
|
||||
println(c format (q, c, q))
|
||||
"""
|
||||
println(c format (q, c, q))
|
||||
2
Task/Quine/Tcl/quine-2.tcl
Normal file
2
Task/Quine/Tcl/quine-2.tcl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
join { {} \{ \} } something
|
||||
=> something{something}
|
||||
2
Task/Quine/Tcl/quine-3.tcl
Normal file
2
Task/Quine/Tcl/quine-3.tcl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
join { {} \{ \} } { join { {} \{ \} } }
|
||||
=> join { {} \{ \} } { join { {} \{ \} } }
|
||||
34
Task/Quine/Tcl/quine-4.tcl
Normal file
34
Task/Quine/Tcl/quine-4.tcl
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
program quine;
|
||||
|
||||
const
|
||||
apos: Char = Chr(39);
|
||||
comma: Char = Chr(44);
|
||||
lines: Array[1..17] of String[80] = (
|
||||
'program quine;',
|
||||
'',
|
||||
'const',
|
||||
' apos: Char = Chr(39);',
|
||||
' comma: Char = Chr(44);',
|
||||
' lines: Array[1..17] of String[80] = (',
|
||||
' );',
|
||||
'',
|
||||
'var',
|
||||
' num: Integer;',
|
||||
'',
|
||||
'begin',
|
||||
' for num := 1 to 6 do writeln(lines[num]);',
|
||||
' for num := 1 to 16 do writeln(apos, lines[num], apos, comma);',
|
||||
'% writeln(apos, lines[17], apos);',
|
||||
' for num := 7 to 17 do writeln(lines[num]);',
|
||||
'end.'
|
||||
);
|
||||
|
||||
var
|
||||
num: Integer;
|
||||
|
||||
begin
|
||||
for num := 1 to 6 do writeln(lines[num]);
|
||||
for num := 1 to 16 do writeln(apos, lines[num], apos, comma);
|
||||
writeln(apos, lines[17], apos);
|
||||
for num := 7 to 17 do writeln(lines[num]);
|
||||
end.
|
||||
2
Task/Quine/Tcl/quine.tcl
Normal file
2
Task/Quine/Tcl/quine.tcl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
join { {} A B } any_string
|
||||
=> any_stringAany_stringB
|
||||
Loading…
Add table
Add a link
Reference in a new issue