Quine: Difference between revisions
(Haskell example added, implements the obvious generic solution) |
(Add Seed7 example) |
||
Line 111: | Line 111: | ||
(The X-Powered-By line will vary from system to system.) |
(The X-Powered-By line will vary from system to system.) |
||
=={{header|Seed7}}== |
|||
$ include "seed7_05.s7i"; |
|||
const array string: prog is []( |
|||
"$ include \"seed7_05.s7i\";", |
|||
"const array string: prog is [](", |
|||
"const proc: main is func", |
|||
" local var integer: number is 0;", |
|||
" begin", |
|||
" for number range 1 to 2 do writeln(prog[number]); end for;", |
|||
" for number range 1 to 11 do", |
|||
" writeln(literal(prog[number]) <& \",\");", |
|||
" end for;", |
|||
" writeln(literal(prog[12]) <& \");\");", |
|||
" for number range 3 to 12 do writeln(prog[number]); end for;", |
|||
" end func;"); |
|||
const proc: main is func |
|||
local var integer: number is 0; |
|||
begin |
|||
for number range 1 to 2 do writeln(prog[number]); end for; |
|||
for number range 1 to 11 do |
|||
writeln(literal(prog[number]) <& ","); |
|||
end for; |
|||
writeln(literal(prog[12]) <& ");"); |
|||
for number range 3 to 12 do writeln(prog[number]); end for; |
|||
end func; |
|||
Original source: [http://seed7.sourceforge.net/algorith/puzzles.htm#self] |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
Revision as of 12:38, 20 November 2007
Print out a program's own source code. Since this is a puzzle, shorter/more efficient versions are encouraged along side more obvious versions.
Ada
with Ada.Text_Io; use Ada.Text_Io; procedure Quine is Line : String(1..80); Length : Natural; File : File_Type; begin Open(File => File, Name => "Quine.adb", Mode => In_File); while not End_Of_File(File) loop Get_Line(File => File, Item => Line, Last => Length); Put_Line(Line(1..Length)); end loop; end Quine;
The following example is a quine without reading the source file.
with Text_Io; procedure Self is Q:Character:='"'; A:String(1..132):="with text_io;procedure self is q:character:=;a:string(1..132):=;begin text_io.put_line(a(1..45)&q&a(46..65)&q&a&q&a(66..132));end;"; begin Text_Io.Put_Line(A(1..45)&Q&A(46..65)&Q&A&Q&A(66..132)); end;
Befunge
The code space is also the data space of a Befunge program. Programs can be read and modified on the fly. This quine works by reading and printing each character of the source. (This is a implicit loop, since the Befunge codespace wraps around.)
:0g,:66+`#@_1+
Forth
SOURCE TYPE
Haskell
The obvious solution, as a one-liner:
let q s = putStrLn $ s ++ show s in q "let q s = putStrLn $ s ++ show s in q "
Having the string at the end makes things a bit simpler than in generic version mentioned in the Wikipedia article.
Haskell does not keep the code in an uncompiled-equivalent form around at runtime, so the "quotation trick" has to be used.
Java
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Quine{ public static void main(String[] args){ try{ BufferedReader input = new BufferedReader( new FileReader("Quine.java")); String line; while((line = input.readLine())!=null){ System.out.println(line); } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }
Perl
This example retrieves the name of the source file from the $0 variable, which represents the file path and name used to invoke the script.
#!/usr/bin/perl open FILE, "< $0" or die "Failed to open self\n"; map { print $_ } <FILE>; close FILE;
Assuming the code is saved in the file quine.pl, this works when executed as
./quine.pl
or
perl quine.pl
but fails when fed to the perl interpreter's standard input as
cat quine.pl|perl
If we may assume that the source file is named "quine.pl", replacing
open FILE, "< $0" or die "Failed to open self\n";
with
open FILE, "< quine.pl" or die "Failed to open self\n";
will allow us to execute the code through the interpreter's standard input:
cat quine.pl|perl
PHP
This example obtains its own filename from the __FILE__ built-in variable.
<? $fh = fopen( __FILE__, 'r' ); echo fread($fh, filesize(__FILE__)); fclose( $fh ); ?>
Strictly speaking, however, the example prints more than its own source code. Because PHP is designed to service HTTP requests, the output looks like this:
X-Powered-By: PHP/5.2.3-1ubuntu6 Content-type: text/html <? $fh = fopen( __FILE__, 'r' ); echo fread($fh, filesize(__FILE__)); fclose( $fh ); ?>
(The X-Powered-By line will vary from system to system.)
Seed7
$ include "seed7_05.s7i"; const array string: prog is []( "$ include \"seed7_05.s7i\";", "const array string: prog is [](", "const proc: main is func", " local var integer: number is 0;", " begin", " for number range 1 to 2 do writeln(prog[number]); end for;", " for number range 1 to 11 do", " writeln(literal(prog[number]) <& \",\");", " end for;", " writeln(literal(prog[12]) <& \");\");", " for number range 3 to 12 do writeln(prog[number]); end for;", " end func;"); const proc: main is func local var integer: number is 0; begin for number range 1 to 2 do writeln(prog[number]); end for; for number range 1 to 11 do writeln(literal(prog[number]) <& ","); end for; writeln(literal(prog[12]) <& ");"); for number range 3 to 12 do writeln(prog[number]); end for; end func;
Original source: [1]
UNIX Shell
#!/bin/sh cat "$0"