本文介绍了构造quines(自我复制功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以构造一个quine(一个程序,它会生成自己的源文本的副本作为其完整输出": http://www.nyx.net/~gthompso/quine.htm )中的R? ([quine]标签在Python,Java等中引出了很多示例,但在R中显然没有.)

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() }

即将结束:

> f()
{
    body()
}

但缺少函数的名称.

最短的可能性怎么样?最容易混淆?

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:

  • 在R环境中:函数->函数(@ bill_080)
  • 在OS/shell环境中:程序->程序[或多或少等同于程序->文本]:(@kohske)
  • other:函数->文本(@ JoshUlrich,@ James,问题如上定义)
  • 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)

注意:

  • @Spacedman指出的R-help的线程(似乎强调简短而不是混淆)表明identical(quine,quine())是一个很好的测试用例,尽管它很棘手,因为环境会随身携带:identical(quine,quine(),ignore.environment=TRUE)可能会更容易.
  • 最近(2015年10月)博客帖子提供了另一个答案...
  • 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 ...

推荐答案

这是我能想到的最短的方法:

This is the shortest I can come up with:

> "f" <- function() call("<-", "f", f)
> f()
"f" <- function () 
call("<-", "f", f)

这篇关于构造quines(自我复制功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:27