什么是quines?拥有它们的任何特定目的?

Arn*_*shn 18 python quine

我遇到了这个术语 - 奎因(也称为自我复制程序).只是想了解更多信息.如何写一个quine,他们在任何地方使用,或者他们只是一个有趣的运动?

我从Python开始,我可能会尝试用Python编写一个.有什么建议?

Alg*_*ias 28

奎因在实际意义上是无用的,但它们是一个很好的练习,可以帮助你更多地了解一门语言.

这是python中非常简洁的一个:

a='a=%r;print a%%a';print a%a
Run Code Online (Sandbox Code Playgroud)

  • 我还没有看到最短版本的+1. (5认同)
  • 也.format()版本而不是弃用%:`a ='a = {}; print(a.format(repr(a)))'; print(a.format(repr(a)))` (3认同)

dmc*_*kee 19

quines至少是生成自己的源作为输出的程序.它们是构建哥德尔关于不完整性证明的必要步骤.

这是否构成实际用途是我不提供评论的内容.


Mar*_*iek 10

quine是一个计算机程序,它将自己的源代码副本作为唯一的输出.

我还没有看到一个实际用途,但我确定那里有一个.


Python示例(在此处找到)

print (lambda s:s+`s`+')')("print (lambda s:s+`s`+')')(")
Run Code Online (Sandbox Code Playgroud)

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. */
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*dol 8

正如其他人所解释的那样,quines是可以复制自己的精确副本的程序.

关于应用程序,如果你认为DNA编码逻辑来解释自身并重现自己 - 答案是非常简单的,没有quines的概念我们就不会在这里,我们将永远无法创造人工(自我复制) )生活.


sal*_*sal 5

这是我最喜欢的C例子

char*p="char*p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Run Code Online (Sandbox Code Playgroud)

我从中学到了两件事:

  1. 不需要空白区域,但确实有助于提高可读性
  2. prinftf功能非常强大