Question

I came across this term - Quine (also called self-reproducing programs). Just wanted to know more on it. How does one write a quine and are they used anywhere or they are just an exercise for fun?

I've started with Python, and I might try writing one in Python. Any suggestions?

Was it helpful?

Solution

Quines are useless in a practical sense, but they're a great exercise to help you learn more about a language.

Here's a very concise one in python:

a='a=%r;print a%%a';print a%a

OTHER TIPS

At a minimum, quines are programs which produce their own source as their output. They are a necessary step in constructing Gödel's proof on incompleteness.

Whether this constitutes a practical use is something I offer no comment on.

A quine is a computer program which produces a copy of its own source code as its only output.

I've yet to see a practical use for one but I'm sure there's one out there somewhere.


Python Example (found here)

print (lambda s:s+`s`+')')("print (lambda s:s+`s`+')')(")

C Example (found here

#include <stdio.h>

int main(int argc, char** argv)
{
/* This macro B will expand to its argument, followed by a printf
 command that prints the macro invocation as a literal string */
#define B(x) x; printf("  B(" #x ")\n");

/* This macro A will expand to a printf command that prints the
 macro invocation, followed by the macro argument itself. */
#define A(x) printf("  A(" #x ")\n"); x;

/* Now we call B on the text of the program
 up to this point. It will execute the command, and then cause
 itself to be printed. */
  B(printf("#include <stdio.h>\n\nint main(int argc, char** argv)\n{\n/*
    This macro B will expand to its argument, followed by a printf\n
    command that prints the macro invocation as a literal string
    */\n#define B(x) x; printf(\"  B(\" #x \")\\n\");\n\n/* This macro
    A will expand to a printf command that prints the\n
    macro invocation, followed by the macro argument itself. */\n#define A(x)
    printf(\"  A(\" #x \")\\n\"); x;\n\n/* Now we call B on the text
    of the program\n up to this point. It will execute the command,
    and then cause\n itself to be printed. */\n"))
  A(printf("/* Lastly, we call A on a command to print the remainder
    of the program;\n it will cause itself to be printed, and then
    execute the command. */\n}\n"))
/* Lastly, we call A on a command to print the remainder of the program;
 it will cause itself to be printed, and then execute the command. */
}

As others explained, quines are programs that reproduce exact copies of themselves.

With regards to applications, if you think that the DNA encodes logic to interpret itself and reproduce itself - the answer is pretty straightforward, without the concept of quines we wouldn't be here and we would never be able to create artificial (self-reproducing) life.

This is my favorite C example

char*p="char*p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}

Two things I learned from it:

  1. White space isn't required but does help readability
  2. The prinftf function is really powerful

I can't present any data to say that writing a quine or two has expanded my mind or made me a better programmer. But it is fun to do, at least the first couple of times. Anyway, you asked about how to write one. I can point you to some well written references:

Craig Kaplan has a neat paper which describes how to actually produce quines:

  • The Search For Self-Documenting Code
    • This report examines the problem of writing a self-documenting program: a program that, when run, produces itself as output. The problem is examined from the point of view of self-reference, the property a self-documenting program must exhibit. The report proceeds from early programs that fail to work correctly, through successively sophisticated programs which approach a solution, to working self-documenting programs. Then it steps back a bit and shows how some programs can seem to cheat and still fit the definition of a self-documenting program, suggesting improvements to that definition. At each step, the report addresses how the given programs demonstrate the subtle relationship between computer programming and self-reference.

You might also find David Madore's "Quines (self-replicating programs)" interesting reading.

Finally, if you want to see implementations, check out the Quine Page where you can find quines in various languages and other related matter.

What are quines used for? Programming exercises and viruses.

A virus needs to replicate somehow -- and one way is to make it a quine. Let's say that a hypothetical antivirus program would flag any process that read its own binary into memory (to pass it to the intended victim); the way to get around that would to have it output itself.

Bear in mind that a quine in machine code would require no compilation.

Here's one in Python (it's ugly; I just wrote it to try it out). Didn't even know this was called a quine back then.

def e(s): print s[:42]+s[42:].replace('#','"'); print 'e("""'+s+'""")'
e("""def e(s): print s[:42]+s[42:].replace('#','"'); print 'e(###'+s+'###)'""")

Oh, and to answer your other question: Quines are totally useless.

I wrote my first Quine in 1979 -- in Fortran. I has a random thought the other day about Quines in PHP and felt like posting the same Q as the OP but being a good boy I first checked the Q&A D/B. Anyway for posterity here is my PHP(cli) quine. I'd be interest in any shorter variants. :-)

<?php $x='<?php $x=0;echo strtr( $x, array(chr(39).$x.chr(39)));';echo strtr( $x, array(chr(39).$x.chr(39)));

109 bytes, but with the last CR cut off. That's not counting the "cheat":

<?php readfile( __FILE__);

And this QuineProgram wiki quotes an even shorter one:

<?php printf($a='<?php printf($a=%c%s%c,39,$a,39);',39,$a,39);

This is an interesting quine in c++: http://npcomplete.weebly.com/1/post/2010/02/self-reproducing-c-program-quine.html

Quines are for fun. They have no practical use as far as I know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top