构造奎因(自我复制功能)
有没有人构建过 quine(“生成自己源文本的副本作为其完整输出的程序”:http://www.nyx.net/~gthompso/quine.htm)在 R 中? ([quine] 标签在 Python、Java 中提取了大量示例,...但显然在 R 中没有。)
f <- function() { body() }
很接近:
> f()
{
body()
}
但缺少函数名称。
最短的可能性怎么样?最困惑?
编辑:从下面的各种答案来看,似乎有多种方法可以定义自引用及其必须发生的环境:
- 在R环境中:函数
-> ;操作系统/shell 环境中的
函数 (@bill_080) - :program
->
程序 [或多或少等同于program->
文本]: ( @kohske) - 其他:功能
->
文本(@JoshUlrich、@James,问题如上定义)
注释:
- @Spacedman 指出的 R-help 线程(似乎强调混淆而不是简洁)表明
identical(quine,quine())
是一个很好的测试用例,尽管它很棘手,因为环境会一直进行:identical(quine,quine(),ignore.environment=TRUE)
可能会更容易。 - 最近(2015 年 10 月)博客文章提供了另一个答案......
Has anyone constructed a quine ("A program that generates a copy of its own source text as its complete output": http://www.nyx.net/~gthompso/quine.htm) in R? (The [quine] tag pulls up lots of examples in Python, Java, ... but apparently none in R.)
f <- function() { body() }
comes close:
> f()
{
body()
}
but lacks the name of the function.
How about the shortest possibility? Most obfuscated?
edit: from the variety of answers below, it seems that there are a variety of ways to define self-referentiality and the environment in which it must occur:
- within the R environment: function
->
function (@bill_080) - within the OS/shell environment: program
->
program [more or less equivalent to program->
text]: (@kohske) - other: function
->
text (@JoshUlrich, @James, problem as defined above)
Notes:
- The thread from R-help pointed out by @Spacedman (which seems to emphasize obfuscation over brevity) suggests that
identical(quine,quine())
is a good test case, although it's tricky because environments get carried along:identical(quine,quine(),ignore.environment=TRUE)
might be easier. - A recent (Oct 2015) blog post provides another answer ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我能想到的最短的:
This is the shortest I can come up with:
这是一个真正的奎因,一个程序(不是函数),它生成自己的源文本的副本作为其完整的输出。
在控制台上,
这可能不是最短的。
更新:
和稍短的版本:
Here is a real Quine, a program (not a function) that generates a copy of its own source text as its complete output.
On console,
probably this is not the shortest one.
UPDATED:
and slightly shorter version:
以
body
的作用为灵感,call
可用于重现调用命令:输出:
Using what
body
does as inspiration,call
can be used to reproduce the calling command:Which outputs:
如果你想要一个返回函数的函数......也许是这个?
输出是:
If you want a function that returns a function.....maybe this?
The output is:
虽然我不确定从 quine 的角度来看这是否“重要”(我在尝试验证是否有效时偶然发现了这个问题),但脚本
将输出
function(){}
。这与 Joshua Ulrich 的答案的原理相同,只是精简到了要点。While I'm not sure if this "counts" from a quine perspective (I stumbled across this question while trying to verify if it does), the script
will output
function(){}
. This works on the same principle as Joshua Ulrich's answer, just pared down to the essentials.@Spacedman 的回答,来自 r-help 邮件列表2003(线程中还有更多示例):
这通过了 quine 脚本测试:
我确信如果我理解它,我可以做得更好...
原始 javascript 版本作者:Geoffrey A Swift(blimey at toke.com)
来自:http://www.nyx.net/~gthompso/quine.htm
An answer from @Spacedman, from the r-help mailing list in 2003 (there are more examples in the thread):
This passes the quine script test:
I'm sure if I understood it I could make it better...
Original javascript version by: Geoffrey A Swift (blimey at toke.com)
From: http://www.nyx.net/~gthompso/quine.htm