Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A metaquine is a program which is not a quine, but whose output, when run as a program in the same language, is a quine.

The goal of this challenge is to write a metaquine. This is , so shortest code wins, with earliest answer used as a tiebreaker. Note that only full programs are acceptable, due to the definition of a quine.

Rules for Quines

Only true quines are accepted. That is, you need to print the entire source code verbatim to STDOUT, without:

  • reading your source code, directly or indirectly.
  • relying on a REPL environment which just simply evaluates and prints every expression you feed it.
  • relying on language features which just print out the source in certain cases.
  • using error messages or STDERR to write all or part of the quine. (You may write things to STDERR or produce warnings/non-fatal errors as long as STDOUT is a valid quine and the error messages are not part of it.)
  • the source code consisting purely of literals (whether they be string literals, numeric literals, etc.) and/or NOPs.

Any non-suppressible output (such as copyright notices, startup/shutdown messages, or a trailing line feed) may be ignored in the output for the sake of the validity of the quine.

Example

Ignoring the rule which forbids literal-only programs and built-in quining, this would be a metaquine in Seriously:

"Q"

The program consists of the single string literal "Q", which is implicitly printed at output. When the output (Q) is run, it is a quine (Q is the built-in quine function).

share|improve this question
2  
Does the literals/comments/NOPs rule apply to the output (i.e. the real quine) of the metaquote as well? Otherwise ex. T is an easy 1-byte Pyth answer. – Doorknob Feb 12 at 18:23
    
@Doorknob Yes, I'll clarify. – Mego Feb 12 at 18:25
7  
I don't really see where's the difficult point of this challenge. Isn't the strategy "print the string which contains the shortest known quine" pretty much guaranteed to win for each language? – Fatalize Feb 12 at 18:30
1  
@Fatalize Well I guess the interesting question is, can that be done in the same amount or fewer bytes than the quine itself. – Martin Büttner Feb 12 at 18:53
2  
@Fatalize Isn't it also possible that you could write a short metaquine that prints a long but valid quine? – Rhyzomatic Feb 12 at 19:25
up vote 12 down vote accepted

CJam, 6 bytes

"_p"_p

Prints

"_p"
_p

which is the shortest proper quine in CJam.

Test it here.

Explanation

Both programs work exactly the same, since the linefeed inside the proper quine is a no-op and only included because it's more expensive to remove it from the output. How the programs work:

"_p"  e# Push this string.
_     e# Duplicate it.
p     e# Print a string representation of the top of the stack (i.e. the string with
      e# quotes) followed by a linefeed.
      e# The other copy is then printed automatically at the end of the program, without
      e# stringifying it.

Side Note

The same works in GolfScript as

".p".p

which prints

".p"
.p

with a trailing linefeed, which in turn is one of the shortest known quines.

share|improve this answer

Pyth, 12 11 10 9 bytes

Knocked off one more byte thanks to @Pietu1998.

jN B".[9N

This prints

.[9N".[9N

which is a quine in Pyth. You can try it out here.

share|improve this answer
    
Is the output a new record for the shortest Pyth quine? I don't believe I've ever seen it before. – ETHproductions Feb 12 at 21:08
    
I couldn't find it anywhere else. I came up with it trying to get a metaquine. – Rhyzomatic Feb 12 at 21:11
    
Perhaps you should post it as an answer to the original quine challenge :) – ETHproductions Feb 13 at 1:08
2  
You can get this down to 9 by using jN B".[9N or .[9N"jN B. (jN B"jN B is another true quine at 9: bifurcate identity function and join by ".) – Pietu1998 Feb 13 at 1:36
    
@Pietu1998 Thanks! I knew there must be a way to get it to 9. Are there any quines in Pyth that are shorter than 9 bytes? – Rhyzomatic Feb 13 at 5:50

Fission, 6 bytes

!+OR"'

Prints

'!+OR"

Which is the shortest Fission quine. This works because cyclic shifts of the program leave its output completely unaffected.

Try it online!

share|improve this answer

Japt, 15 13 bytes

Q+"+Q ³s7J" ²

Test it online!

This program outputs

"+Q ³s7J"+Q ³s7J

which is the shortest known quine in Japt.

How it works

Q+"..." // Take a quotation mark plus this string.  "+Q ³s7J
²       // Repeat it twice.                         "+Q ³s7J"+Q ³s7J
        // Implicit output

Non-competing version, 11 bytes

Q+"+Q ²é" ²

I've just added é, a "rotate" command. So

"+Q ²é"+Q ²é

is now a valid quine.

share|improve this answer

Javascript ES6, 21 bytes

$=_=>`$=${$};$()`
$()

Trivial.

share|improve this answer
1  
Happy 3k!!!!!!! – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 13 at 1:00
1  
Thanks @CᴏɴᴏʀO'Bʀɪᴇɴ! – Mama Fun Roll Feb 13 at 2:30

Python 2, 29 bytes

_="_=%r;print _%%_";print _%_

Turns out the well-known short python quine is easily turned into a metaquine :)

And, since we don't have to worry about matching the trailing newline, the metaquine is actually shorter!

share|improve this answer

Ruby, 25 23

puts"puts <<2*2,2
"*2,2

Generates the classic Ruby HEREdoc quine

puts <<2*2,2
puts <<2*2,2
2

Old solution

_='_=%p;$><<_%%_';$><<_%_

Generates itself except with the single quotes replaced with double quotes.

share|improve this answer

SMBF, 10 bytes

Also known as Self-modifying Brainf***. \x00 is a null byte, which does not affect the program, nor is it printed. Thus the output is the original quine.

\x00<[<]>[.>]

The tape that the commands modify is the same tape that it operates on. That's part of the specification of the language. It's not just reading a separate copy of its source, and it's not opening its own file. It's looking at its own source dynamically on the same tape, with the power to execute and modify it mid-execution.

This is a trivial adjustment of the shortest quine I know of:

<[<]>[.>]
share|improve this answer
2  
This is not valid because it reads its own source code. Being able to modify it during runtime doesn't make it valid. – Mego Feb 13 at 3:35
1  
If you say so. I had mixed responses on my quine answer – mbomb007 Feb 13 at 3:47
    
In general there isn't a clear consensus on whether we consider Self-Modifying Brainfuck valid for quines. As such, and since this is @Mego's challenge, he should have the say in this case on whether he considers this valid. If he deems it invalid then it should be deleted per our policy on invalid answers. – Alex A. Feb 19 at 22:45

Befunge-93, 22 bytes

"+1_@#`+39:,g0:">:#,_@

I just took the standard quine, put it in quotes, reversed it (so it is loaded on the stack correctly), and then added a stack-print at the end.

It's 8 characters added to the normal quine, so it's very possible there's a better solution.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.