语言特性有助于编写 quines(自打印程序)吗?

发布于 2024-09-06 11:21:40 字数 624 浏览 7 评论 0原文

好的,对于那些从未遇到过这个术语的人来说,quine 是一个“自我复制”的计算机程序。更具体地说,在执行时会生成其自己的源代码的副本作为其唯一的输出。

当然,quines 可以用多种编程语言(但不是全部)来开发;但某些语言显然比其他语言更适合生成奎因(要清楚地理解听起来有点主观的“更适合”,请查看 Wiki 页面中的 Haskell 示例与 C 示例 - 我在下面提供了更客观的定义)。

我的问题是,从编程语言的角度来看,哪些语言特性(理论设计或语法糖)使该语言更适合/有助于编写 quines

我对“更合适”的定义是“quines更容易编写”和“更短/更易读/更少混淆”。但欢迎您添加更多至少在某种程度上客观的标准。

请注意,这个问题明确排除了退化情况,例如设计为包含“print_a_quine”原语的语言。

OK, for those who have never encountered the term, a quine is a "self-replicating" computer program. To be more specific, one which - upon execution - produces a copy of its own source code as its only output.

The quines can, of course, be developed in many programming languages (but not all); but some languages are obviously more suited to producing quines than others (to clearly understand the somewhat subjective-sounding "more suited", look at a Haskell example vs. C example in the Wiki page - and I provide my more-objective definition below).

The question I have is, from programming language perspective, what language features (either theoretical design ones or syntax sugar) make the language more suitable/helpful for writing quines?

My definition of "more suitable" is "quines are easier to write" and "are shorter/more readable/less obfuscated". But you're welcome to add more criteria that are at least somewhat objective.

Please note that this question explicitly excludes degenerate cases, like a language which is designed to contain "print_a_quine" primitive.

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

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

发布评论

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

评论(3

天荒地未老 2024-09-13 11:21:40

我不完全确定,所以如果你们中有人更了解的话请纠正我。
我同意其他两个答案,进一步解释说,奎因是这样的:

Y g

其中 Y 是 Y 定点组合器(或任何其他定点组合器),这意味着 lambda 演算

Y g = g(Y g)

现在,很明显,我们需要代码是数据,g 是一个将打印其参数的函数。

总而言之,我们需要构建这样的 quines 函数、打印函数、定点组合器和按名称调用评估策略。

满足此条件的最小语言是来自 Iota 和 Jot 系列的 AFAIK Zot。

I am not entirely sure, so correct me if anyone of you knows better.
I agree with both other answers, going further by explaining, that a quine is this:

Y g

where Y is a Y fixed-point combinator (or any other fixed-point combinator), which means in lambda calculus:

Y g = g(Y g)

now, it is quite apparent, that we need the code to be data and g be a function which will print its arguments.

So to summarize we need for constructing such a quines functions, printing function, fixed-point combinator and call-by-name evaluation strategy.

The smallest language that satisfies this conditions is AFAIK Zot from the Iota and Jot family.

心在旅行 2024-09-13 11:21:40

Io 编程语言 等语言允许将代码视为数据。在树遍历系统中,这通常允许语言实现者将抽象语法树公开为一等公民。对于木卫一来说,这就是它的作用。 AST 是面向对象的,围绕 Message 对象建模,并创建一个特殊的哨兵来表示当前正在执行的消息;这个哨兵被称为thisMessagethisMessage 是一条完整的消息,与其他消息一样,它响应 print 消息,后者将其打印到屏幕上。结果,我能用任何语言制作的最短的奎因来自木卫一,看起来像这样:

thisMessage print

无论如何,我只是忍不住与你分享这个主题。上面的内容确实使编写 quine 变得容易,但不这样做当然并不妨碍轻松创建 quine。

Languages like the Io Programming Language and others allow the treating of code as data. In tree walking systems, this typically allows the language implementer to expose the abstract syntax tree as a first class citizen. In the case of Io, this is what it does. Being object oriented, the AST is modelled around Message objects, and a special sentinel is created to represent the currently executing message; this sentinel is called thisMessage. thisMessage is a full Message like any other, and responds to the print message, which prints it to the screen. As a result, the shortest quine I've ever been able to produce in any language, has come from Io and looks like this:

thisMessage print

Anyway, I just couldn't help but sharing this with you on this subject. The above certainly makes writing quines easy, but not doing it this way certainly doesn't preclude easily creating a quine.

初心 2024-09-13 11:21:40

我不确定从实践的角度来看这是否是有用的答案,但是可计算性理论中有一些有用的理论。特别是不动点和克莱恩递归定理可用于编写奎因。显然,该理论可用于在 LISP 中编写 quine (如维基百科页面所示)。

I'm not sure if this is useful answer from a practical point of view, but there is some useful theory in computability theory. In particular fixed points and Kleene's recursion theorem can be used for writing quines. Apparently, the theory can be used for writing quine in LISP (as the wikipedia page shows).

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