13

After poking around on the internet I wasn't able to find anyone who had written a quine in R (Edit: since writing this, have found several on SO, but am still interested in this one). So I figured I'd try my hand at coming up with one myself. My result was the (surprisingly short) code:

function(){}

which will output function(){} when run. This takes advantage of the fact that a function name without parens or arguments after it will return the function's source code.

However, a program that "looks at itself" is not generally considered a true quine. There are two things I realized I don't understand in the course of trying to decide whether I'd written a "real" quine: (1) What constitutes a program "looking at itself" (from a quine standpoint) beyond use of file i/o and (2) the extent to which function(){} (or similar commands like logical(0)) are self referential when they print themselves. The former seems to be too subjective for SO, but I was hoping for some clarification on the latter. So...

When I run function(){}, what exactly is happening that causes it to print its own "source code"? For example, is R loading an empty function into a local environment, evaluating that function, and then looking back at the code that defined it to print? Or, is it just looking at function(){} and echoing its definition right away? Is there a fundamental difference between this and

f<-function(){cat("f<-");print(f);cat("f()")}
f()

in terms of how they both print themselves when run?

Empiromancer
  • 3,778
  • 1
  • 22
  • 53
  • 5
    I think it would be cheating because the REPL is implicitly calling `print()` for you; your source code is not printing itself. – MrFlick Nov 03 '15 at 21:57
  • 1
    I've never heard the word 'quine', but R functions are fun, maybe something like `f <- (function() this <- function() print(attr(this,'srcref')))()` then `f()` – Rorschach Nov 03 '15 at 22:00
  • 1
    Any anonymous function definition is a quine following your logic. Actually, the function you are defining is not executed, but just printed, as @MrFlick said. – nicola Nov 03 '15 at 22:00
  • 1
    or maybe `f <- function() print(attr(get('f'), 'srcref'))` would make more sense – Rorschach Nov 03 '15 at 22:02
  • R tends to print a lot of things verbatim, `NULL`, `espression(1)`, `character(0)`, so any of these could be put in a script on their own and their "output" (in the console) would consist of their source code. Ensuring that the output is a separate file... now there's more of a puzzle. – Frank Nov 03 '15 at 22:02
  • 6
    This is interesting but I'm not sure it's on-topic: it's not a programming question, but a question about the *definition* of a quine, which is prone to opinionated answers ... more discussion [here](http://stackoverflow.com/questions/6495962/constructing-quines-self-reproducing-functions) – Ben Bolker Nov 03 '15 at 22:04
  • I wrote a quine in R [here](http://xavier.nayrac.eu/2015/10/13/a-quine-n-r-the-return/) and there is another one on rosetta code – Xavier Nayrac Nov 11 '15 at 06:52

3 Answers3

1

You don't completely get what's going on here. Actually, the code

function(){}

doesn't do anything apart from constructing a function without arguments and body, returning it and deleting it immediately after it's returned. Its output would be NULL, so doesn't "recreate itself".

The output you see in the console, is not the output given by function(){} but by print.function. This is the S3 method that takes care of showing a function object in the console. What you actually do, is:

a <- function(){}
print(a)
rm(a)

A true R quine would be something like this:

m<-"m<-0;cat(sub(0,deparse(m),m))";cat(sub(0,deparse(m),m))

See also Wikipedia for this and other examples

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
0

This is not a true quine as it does not print anything to stdout. Whole point of Quine is that it can reproduce itself by printing. Program must create a new file or output in stdout containing its exact code.

Example of a javascript quine would be:

(function a(){console.log(`(${a}())`)}())
Nikhil Ranjan
  • 994
  • 12
  • 16
0

(function(x) print(substitute(x(x))))(function(x) print(substitute(x(x))))

Ric
  • 5,362
  • 1
  • 10
  • 23
  • Is this an example quine? I don't think the question was looking for a valid quine but whether their particular program counted – qfwfq Oct 31 '22 at 16:58
  • I agree you're right, nevertheless i'm not sure if this question is not off topic. – Ric Oct 31 '22 at 18:18