tags:

views:

572

answers:

11

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?

+8  A: 

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. */
}
Mark Biek
+1  A: 

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
+13  A: 

At a minimum, quines---programs which produce their own source as their output---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.

dmckee
+13  A: 

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
+1 for the shortest version I have yet to see.
MitMaro
It also converts easily to Python 3.x:a='a=%r;print(a%%a)';print(a%a)
dan04
+2  A: 

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
A: 

Szymon Rusinkiewicz's quine (which was originally written in C) should work.

Andrew Grimm
A: 

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
a quine in **machine-code** would require no compilation
Blorgbeard
Right, that. Edited.
ojrac
+3  A: 

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
+2  A: 

See Reflections on Trusting Trust for an example of a trojan using a quine.

sdcvvc
+3  A: 

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
A: 

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