19

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?

Arnkrishn
  • 29,828
  • 40
  • 114
  • 128

10 Answers10

29

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
Algorias
  • 3,043
  • 5
  • 22
  • 16
19

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
11

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. */
}
Community
  • 1
  • 1
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
9

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.

JohnIdol
  • 48,899
  • 61
  • 158
  • 242
6

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
sal
  • 23,373
  • 15
  • 66
  • 85
4

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.

ars
  • 120,335
  • 23
  • 147
  • 134
2

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.

ojrac
  • 13,231
  • 6
  • 37
  • 39
1

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.

balpha
  • 50,022
  • 18
  • 110
  • 131
1

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);
TerryE
  • 10,724
  • 5
  • 26
  • 48
0

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.

abc
  • 1,352
  • 1
  • 9
  • 13