News for hackers. Stuff that matters.
Re:Quines: self-replicating programs (2005) by xamuel on Monday May 25, 2020 @10:13PM (#23306825)
You can do this way easier if the language has an "exec" function. Here's a python example:

  x="""def f():
    print("x=" + '"'*3 + x + '"'*3)
    print("exec(x)")
    print("f()")"""
  exec(x)
  f()
Re:Quines: self-replicating programs (2005) by junke on Tuesday May 26, 2020 @12:36AM (#23307470)
Another way of cheating, with reader macros in Common Lisp:

    USER> #1=(LAMBDA () (WRITE '#1# :CIRCLE T) NIL)                                                                                                                                                
    #<FUNCTION (LAMBDA ()) {540F153B}>                                                                                                                                                            
The function, when evaluated, prints its code:

    USER> (funcall *)                                                                                                                                                                             
    #1=(LAMBDA () (WRITE '#1# :CIRCLE T) NIL)                                                                                                                                                     

If you read back the printed string, you have an identical function as the first one. The '#n=' binds a reader label n to the AST that follows, and '#n#' is replaced by the AST bound to n, which means your AST can be a circular data-structure. I added :circle t so that circular structure are printed readably, and the (write ...) is followed by NIL so that the structure is not printed to the REPL during interactive evaluation.

You could also write:

    #1=(lambda () (eval '#1#))
When you evaluate the expression you have a #1={function, which, when called, returns a #1#}.
Re:Quines: self-replicating programs (2005) by klyrs on Monday May 25, 2020 @10:20PM (#23306876)
People consider this cheating but idk

  print(open(__file__).read())
Re:Quines: self-replicating programs (2005) by nuclearnice1 on Monday May 25, 2020 @11:44PM (#23307209)
What’s not to know?
Re:Quines: self-replicating programs (2005) by klyrs on Tuesday May 26, 2020 @02:04PM (#23314404)
... if I agree with the consensus about what constitutes "cheating." For example the wikipedia page suggests that "eval" is okay but accessing the filesystem is not -- folks here seem to regard eval as cheating for some reason that escapes me. These decisions seem arbitrary to me
Re:Quines: self-replicating programs (2005) by nuclearnice1 on Tuesday May 26, 2020 @06:05PM (#23316971)
They are arbitrary.

They are a matter of taste.

Eval. Reading the source.

These are quines by some narrow technical definitions which skates along the surface, but misses all of the beauty.

Maybe the aren’t cheating. But they aren’t special. They might not be disqualified, but they score — at best — 2/10.

Re:Quines: self-replicating programs (2005) by xamuel on Tuesday May 26, 2020 @11:56PM (#23319245)
Beauty is in the eye of the beholder. I actually find eval-based quines MORE beautiful because they highlight the philosophically interesting aspects of quines, such as...

* ...the blurring of the distinction between output and code.

* ...the distinction between name and thing-being-named. (The different "x"'s in my example, within different levels of nested quotes.)

* ...leaky context (local variables defined inside the eval scope ending up outside the eval scope, and vice versa).

Harder quines tend to hide all that behind layers of tedious encoding.

If you're really interested in hard quines, you might be interested in trying to dig up some fully-detailed proofs of Godel's incompleteness theorems. It's the same story there: elegant and beautiful ideas, drowned under mountains of tedious encoding.

Re:Quines: self-replicating programs (2005) by nuclearnice1 on Wednesday May 27, 2020 @08:05AM (#23321991)
Very good points on eval. Can the read() example be similarly redeemed?
Re:Quines: self-replicating programs (2005) by xamuel on Wednesday May 27, 2020 @09:53PM (#23332562)
Good question, I had to think about it for awhile. I think it would take some elaborate mental gymnastics to try to redeem the read example. The read example is sort of like an appeal to a higher-power oracle. It isn't self-contained; it wouldn't work in the REPL.

As a compromise, one could imagine a quine which works by writing programs to disk and then reading those programs (as opposed to reading itself). This would be kind of silly, since the same data could be written to/read from RAM much easier.

Re:Quines: self-replicating programs (2005) by 0xdeadb00f on Tuesday May 26, 2020 @02:53AM (#23308175)
> ...but idk [if it really is cheating]

Maybe that's what they meant

Re:Quines: self-replicating programs (2005) by identity0 on Monday May 25, 2020 @10:20PM (#23306871)
I mean, obviously. I would say it counts as cheating though. It’s on the same level as opening the source code file and printing it out.
Re:Quines: self-replicating programs (2005) by xamuel on Monday May 25, 2020 @10:23PM (#23306888)
No it's not on the same level, it's subtly different. The file could be altered during the brief instant between when you execute the file and when the program loads itself. Or the whole thing could be run from the REPL with no "file" at all.
Re:Quines: self-replicating programs (2005) by identity0 on Monday May 25, 2020 @10:29PM (#23306912)
Not cheating as in “it might not work,” cheating as in “it trivializes the exercise.” The point of making a quine is a programming challenge. If python had a function called “quine()”, I would still call it cheating even if it worked 100% of the time.
Re:Quines: self-replicating programs (2005) by xamuel on Monday May 25, 2020 @10:35PM (#23306931)
Sure, which is why I said "way easier". If you were assigning quine as a python homework exercise, and you wanted an "exec" solution ruled out, you would need to explicitly rule it out in the text of the exercise.

>If python had a function called "quine()"...

If we wanted to REALLY trivialize it, just use the blank program (which prints nothing, thus prints itself). :)