Jump to content

Quine

From Rosetta Code
Revision as of 02:00, 30 November 2007 by 68.225.150.119 (talk) (Added Python example)
Quine is a programming puzzle. It lays out a problem which Rosetta Code users are encouraged to solve, using languages and techniques they know. Multiple approaches are not discouraged, so long as the puzzle guidelines are followed. For other Puzzles, see Category:Puzzles.
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task.


A Quine is a self-referential program that can, without any external access, output its own source. It is named after the philospher 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."

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 string). 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 toplevel interpreter will print out are not allowed, either.

Ada

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+

C++

#include <iostream>
#include <ostream>

void quote(char const* c)
{
  while (*c)
  {
    switch(*c)
    {
    case '\\':
      std::cout << "\\\\";break;
    case '\n':
      std::cout << "\\n";break;
    case '\"':
      std::cout << "\\\"";break;
    default:
      std::cout << *c;
    }
    ++c;
  }
}

int main()
{
  char const* parts[] = {
    "#include <iostream>\n#include <ostream>\n\nvoid quote(char const* c)\n{\n  while (*c)\n  {\n    switch(*c)\n    {\n    case '\\\\':\n      std::cout << \"\\\\\\\\\";break;\n    case '\\n':\n      std::cout << \"\\\\n\";break;\n    case '\\\"':\n      std::cout << \"\\\\\\\"\";break;\n    default:\n      std::cout << *c;\n    }\n    ++c;\n  }\n}\n\nint main()\n{\n  char const* parts[] = {\n    \"",
    "\",\n    \"",
    "\"\n  };\n  \n  std::cout << parts[0];\n  quote(parts[0]);\n  std::cout << parts[1];\n  quote(parts[1]);\n  std::cout << parts[1];\n  quote(parts[2]);\n  std::cout << parts[2];\n\n  return 0;\n}\n"
  };
  
  std::cout << parts[0];
  quote(parts[0]);
  std::cout << parts[1];
  quote(parts[1]);
  std::cout << parts[1];
  quote(parts[2]);
  std::cout << parts[2];

  return 0;
}

Forth

SOURCE TYPE

Haskell

In Haskell, code need not be nested, which simplifies things:

let q s = putStrLn $ s ++ show s in q "let q s = putStrLn $ s ++ show s in q "

Haskell does not allow to access a source-code equivalent representation of the code at runtime.

Lisp

Lisp has a mechanism to quote code without the need to use strings:

((lambda (x) (list x (list 'quote x)))
 '(lambda (x) (list x (list 'quote x))))

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]


Tcl

There are a number of excellent quines in the Tcl wiki[2], the most useful for real-world programming probably the one that uses [info] to read the source of the currently running script. But that would be like opening its own source file.

The most straightforward one in the spirit of Quine is probably the one that uses [join], which appends the elements in the list given in its first argument with a "joining string" which is given in the second element of the list. For example the three-element list {} A B (the first element of which is an empty list):

join { {} A B  } any_string
=> any_stringAany_stringB 

If "A" and "B" are replaced by literal (i.e. escaped) opening and closing curled braces, the result becomes valid Tcl code:

join { {} \{ \} } something
=> something{something}

and re-assembling these parts with a connecting string that is exactly this operation of re-assembly:

join { {} \{ \} } { join { {} \{ \} } }
=> join { {} \{ \} } { join { {} \{ \} } }

Python

Python's builtin repr() function returns a string containing the source code representation of its argument:

 s = 's = %s\nprint s %% repr(s)'
 print s % repr(s)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.