3 ms·
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
by xamuel 6y ago
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()
- identity0 6y agoI 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.
- xamuel 6y agoNo 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.
- identity0 6y agoNot 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.
- xamuel 6y agoSure, 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). :)
- klyrs 6y agoPeople consider this cheating but idk print(open(__file__).read())
- nuclearnice1 6y agoWhat’s not to know?
- 0xdeadb00f 6y ago> ...but idk [if it really is cheating] Maybe that's what they meant
- klyrs 6y ago... 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
- nuclearnice1 6y agoThey 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.
- xamuel 6y agoBeauty 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.
- junke 6y agoAnother 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#}.