什么是奎因? 拥有它们有什么具体目的吗?

发布于 2024-07-26 11:16:20 字数 1437 浏览 13 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

万劫不复 2024-08-02 11:16:20

奎因在实际意义上没有什么用处,但它们是帮助您更多地了解语言的绝佳练习。

这是一个非常简洁的 python 代码:

a='a=%r;print a%%a';print a%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
沉溺在你眼里的海 2024-08-02 11:16:20

至少,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.

世俗缘 2024-08-02 11:16:20

quine 是一种计算机程序,它生成自己源代码的副本作为其唯一输出。

我还没有看到它的实际用途,但我确信在某个地方有一个。


Python 示例(在此处找到

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

C 示例(在这里找到

#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.


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. */
}
去了角落 2024-08-02 11:16:20

正如其他人所解释的那样,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.

2024-08-02 11:16:20

这是我最喜欢的 C 示例

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

我从中学到了两件事:

  1. 不需要空格,但确实有助于提高可读性
  2. prinftf 函数非常强大

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
岁月苍老的讽刺 2024-08-02 11:16:20

我无法提供任何数据来表明编写一两篇奎因扩展了我的思维或使我成为了一名更好的程序员。 但这样做很有趣,至少在前几次是这样。 不管怎样,你问的是如何写一篇。 我可以向您指出一些写得很好的参考资料:

克雷格·卡普兰(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:

  • 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.

£冰雨忧蓝° 2024-08-02 11:16:20

奎宁有什么用? 编程练习和病毒。

病毒需要以某种方式进行复制——其中一种方法就是使其成为奎因。 假设一个假设的防病毒程序会标记任何将其自己的二进制文件读入内存的进程(将其传递给预期的受害者); 解决这个问题的方法就是让它自己输出。

请记住,机器代码中的 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.

被你宠の有点坏 2024-08-02 11:16:20

这是一个用 Python 编写的(它很难看;我只是为了尝试一下才写它)。 当时根本不知道这叫奎因。

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+'###)'""")

哦,回答你的另一个问题:奎因完全没用。

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.

十雾 2024-08-02 11:16:20

1979 年,我用 Fortran 语言编写了第一个奎因。 前几天我对 PHP 中的奎因斯有一个随机的想法,并想发布与 OP 相同的问题,但作为一个好孩子,我首先检查了 Q&AD/B。 无论如何,对于后代来说,这是我的 PHP(cli) quine。 我会对任何较短的变体感兴趣。 :-)

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

109 字节,但最后一个 CR 被切断。 这还不包括“作弊”:

<?php readfile( __FILE__);

这个 QuineProgram wiki 引用了一个更短的:

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

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);
挽容 2024-08-02 11:16:20

这是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文