Code + data quines
The most general structure for a quine looks something like this pseudocode:
data = "an escaped version of the entire program,
with this string replaced with a marker"
program = data.replace(
an expression that evaluates to the marker but doesn't mention it,
escaped(data))
print program;
This structure can be used to write a (fairly naive) quine in most languages. However, it tends to score fairly badly on most scoring systems, because you have to write the entirety of the program twice. However, most quine structures can be considered optimizations of this one.
There are some subtleties to this. In some languages, the hardest part of
performing this operation is to write the escaping code; in many languages, producing the marker without mentioning its name is difficult; and in some esoteric languages, you'll have to invent your own sort of string literal. All three operations tend not to cause too much trouble, though.
For example, we can write a Python quine escaping a string using repr
, and using the 2-character-sequence x"
string (which is representable as "x\""
, i.e. not using the sequence x"
in the string representation of the string itself) as the marker:
d='d=x"\nprint(str.replace(d,"x\\"",repr(d)))'
print(str.replace(d,"x\"",repr(d)))
2@QPaysTaxes: I aim to make my code here as readable and maintainable as the victory condition allows. Unfortunately, that's still typically indistinguishable from ASCII line noise (or just regular line noise if I'm using Jelly) from people who aren't used to the language. – None – 2017-04-07T07:03:08.867