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.
#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. */
}
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.
#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. */
}
至于应用,如果你认为 DNA 编码逻辑来解释自身并复制自身 - 答案非常简单,如果没有奎因的概念,我们就不会在这里,我们永远无法创造人工(自我复制) ) 生活。
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.
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:
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.
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.
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. :-)
发布评论
评论(10)
奎因在实际意义上没有什么用处,但它们是帮助您更多地了解语言的绝佳练习。
这是一个非常简洁的 python 代码:
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:
至少,quines 是产生自己的源代码作为输出的程序。 它们是构建哥德尔不完备性证明的必要步骤。
这是否构成实际用途我不予评论。
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.
我还没有看到它的实际用途,但我确信在某个地方有一个。
Python 示例(在此处找到)
C 示例(在这里找到
I've yet to see a practical use for one but I'm sure there's one out there somewhere.
Python Example (found here)
C Example (found here
正如其他人所解释的那样,quinine 是能够复制自身的精确副本的程序。
至于应用,如果你认为 DNA 编码逻辑来解释自身并复制自身 - 答案非常简单,如果没有奎因的概念,我们就不会在这里,我们永远无法创造人工(自我复制) ) 生活。
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.
这是我最喜欢的 C 示例
我从中学到了两件事:
This is my favorite C example
Two things I learned from it:
我无法提供任何数据来表明编写一两篇奎因扩展了我的思维或使我成为了一名更好的程序员。 但这样做很有趣,至少在前几次是这样。 不管怎样,你问的是如何写一篇。 我可以向您指出一些写得很好的参考资料:
克雷格·卡普兰(Craig Kaplan)有一篇简洁的论文,描述了如何实际生产奎因:
您还可能会找到 David Madore 的“Quines(自我复制程序)”有趣的阅读。
最后,如果您想查看实现,请查看 Quine 页面,您可以在其中查看查找各种语言的奎因和其他相关内容。
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:
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.
奎宁有什么用? 编程练习和病毒。
病毒需要以某种方式进行复制——其中一种方法就是使其成为奎因。 假设一个假设的防病毒程序会标记任何将其自己的二进制文件读入内存的进程(将其传递给预期的受害者); 解决这个问题的方法就是让它自己输出。
请记住,机器代码中的 quine 不需要编译。
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.
这是一个用 Python 编写的(它很难看;我只是为了尝试一下才写它)。 当时根本不知道这叫奎因。
哦,回答你的另一个问题:奎因完全没用。
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.
Oh, and to answer your other question: Quines are totally useless.
1979 年,我用 Fortran 语言编写了第一个奎因。 前几天我对 PHP 中的奎因斯有一个随机的想法,并想发布与 OP 相同的问题,但作为一个好孩子,我首先检查了 Q&AD/B。 无论如何,对于后代来说,这是我的 PHP(cli) quine。 我会对任何较短的变体感兴趣。 :-)
109 字节,但最后一个 CR 被切断。 这还不包括“作弊”:
这个 QuineProgram wiki 引用了一个更短的:
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. :-)
109 bytes, but with the last CR cut off. That's not counting the "cheat":
And this QuineProgram wiki quotes an even shorter one:
这是 C++ 中的一个有趣的 quine: http://npcomplete.weebly.com/1/post/2010/02/self-reducing-c-program-quine.html
奎因是为了好玩。 据我所知,它们没有实际用途。
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.