views:

708

answers:

8

I think this might be a classic question but I am not aware of an answer. Can a program output a copy of itself, and, if so, is there a short program that does this?

I do not accept the "empty program" as an answer, and I do not accept programs that have access to there own source code. Rather, I am thinking something like this:

int main(int argc, char** argv){ printf("int main(argc, char** argv){ printf...

but I do not know how to continue...

+4  A: 

this is called a quine

sdcvvc
Thanks a lot!! /ragnarius
ragnarius
+24  A: 

It's called a quine, and there's a site that collects them.

PiotrLegnica
Thanks, I will study your link!
ragnarius
[`test`](http://example.com)
PiotrLegnica
@PiotrLegnica: how did you *do* that?
SamB
@SamB: http://meta.stackoverflow.com/questions/46228/markdown-bug-foourl-produces-wrong-output-in-comments
PiotrLegnica
+1  A: 

If you write a quine, be careful that the copies don't also write copies of themselves ad infinitum and end up taking over the world.

Buggieboy
That's the difference between writing a copy and executing it.
Mike Dunlavey
If the copies start reproducing themselves, they will have to be executed.
Buggieboy
It wasn't until this moment that I equated a quine with RNA. Where are the quine vaccines, I ask! Save yourselves, people!
Michael Easter
+2  A: 

In the language invented by Jon Skeet the following operator prints "Hello, world!\n".

h

I can make a modification of this language so that the following program prints "Hello, world!\n":

Hello, world!

So that's the program that prints itself.

Oh, you feel something strange about it, while it has a precise and correct mathematical definition? That's your problem. "I won't accept..." ha! Mathematics does accept, and she's the mistress I serve, so I post this answer.

Pavel Shved
This is profound.
Buggieboy
Yes, and when I think of it I could also imagine a language where the operator h actually outputs h.
ragnarius
This, like using only `H` for Hello World has already been documented in HQ9+, in which `Q` is a command that prints the source code of the program. Also `9` prints the lyrics of '99 bottles of beer on a wall' and `+` increments the accumulator. Coincidentally I used to have a fully functional HQ9+ interpreter on my graphing calculator. :)
Joren
You know, the following program is actually a quine in quite a few programming languages:
SamB
@SamB, true. Perl, Python, Ruby etc. But the OP had something against empty programs, so... :-)
Pavel Shved
+1  A: 

This is indeed a classic question!

Beyond the existence of specific quines, an important result in computability theory is that for any function you might want to compute, there exists a program that "knows its own program text", i.e. that could print itself if desired. This theorem is called Kleene's second recursion theorem.

Martin B
+1  A: 

I assume you allow interpreted languages. (At some level, all languages are interpreted.) Somebody writes the interpreter, and if you are writing it, you can add to it any built-in functions you like, such as a (lispy) function (foo) that does nothing except print "(foo)".

Or you can add a more complex macro-type function (printMeAndMyArgs ...).

So the trick is in how you define the problem.

Mike Dunlavey
+5  A: 

The basic idea of most quines is:

  1. You write code that takes a string literal s and prints it, while replacing occurrences (or the occurrence) of a special substring foo in s by the value of s itself.

  2. You take the entire source code of the program so far and use it as the definition for s. but you exclude the definition of s from the string, instead replacing it by foo.

Well, that's the general idea. The rest is string formatting details, really.

Joren
A: 

Michael Sipser’s “Introduction to the Theory of Computation” explains in one of the chapters how to construct a quine. I have recently written a Java program based on that idea and posted it at : http://bornagainprogrammer.net/2009/11/07/hello-world-from-the-tm-self/

I'd suggest you get hold of that book and try implementing the program yourself in your favorite language. There are lot of other fun theorems in that book.

-kiran

kiran