# Golf you a quine for great good!

Using your language of choice, golf a quine.

A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

No cheating -- that means that you can't just read the source file and print it. Also, in many languages, an empty file is also a quine: that isn't considered a legit quine either.

No error quines -- there is already a separate challenge for error quines.

Points for:

• Smallest code (in bytes)
• Most obfuscated/obscure solution
• Using esoteric/obscure languages
• Successfully using languages that are difficult to golf in

The following Stack Snippet can be used to get a quick view of the current score in each language, and thus to know which languages have existing answers and what sort of target you have to beat:

var QUESTION_ID=69;
var OVERRIDE_USER=98;

getAnswers();var SCORE_REG=(function(){var headerTag=String.raw h\d
var score=String.raw \-?\d+\.?\d*
var normalText=String.raw [^\n<>]*
var strikethrough=String.raw <s>${normalText}</s>|<strike>${normalText}</strike>|<del>${normalText}</del> var noDigitText=String.raw [^\n\d<>]* var htmlTag=String.raw <[^\n<>]+> return new RegExp(String.raw <${headerTag}>+String.raw \s*([^\n,]*[^\s,]),.*?+String.raw (${score})+String.raw (?=+String.raw ${noDigitText}+String.raw (?:(?:${strikethrough}|${htmlTag})${noDigitText})*+String.raw </${headerTag}>+String.raw ))})();var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}
body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)
if(languages.hasOwnProperty(lang))
langs.push(languages[lang]);langs.sort(function(a,b){if(a.uniq>b.uniq)return 1;if(a.uniq<b.uniq)return-1;return 0});for(var i=0;i<langs.length;++i)

• Do you not mean, "Golf you a quine for greater good!"? – Mateen Ulhaq May 3 '11 at 2:49
• @muntoo it's a play on "Learn you a Haskell for Great Good". – Rafe Kettler May 3 '11 at 2:52

# Funky, 21 bytes

f=@writef=[f]f()f()

Try it online!

or, if Functions are allowed...

# Funky, 9 bytes

f=@'f='+f

The second of these defines a function f which returns the string f=@'f='+f, the first however is a full program.

Try it online!

# Aubergine, 21 bytes

-a1+a1=oA=Bi-BA:bB=ia

This program ends with a trailing tab character.

• Tab character?. – CalculatorFeline Jan 26 '17 at 21:47
• I wrote this program in November 2015. I could have sworn I'd posted it here already. Anyway thanks for posting it and no thanks for not giving me credit. – quintopia Dec 15 '17 at 6:37
• That doesn't make sense, you didn't post it on here, so how would I know you already wrote it lmao – Oliver Ni Dec 16 '17 at 7:02

# Locksmith, 201 bytes

070405000400080701090704000102010702070000080006030109000107020001020106070707040507040001020107020700000800060301090001070200010201067450408719740121727008063190172012167774574012172700806319017201216

Try it online!

Formatted:

0704050004000807010907040001020107020700000800060301090001070200010
2010607070704050704000102010702070000080006030109000107020001020106
7450408719740121727008063190172012167774574012172700806319017201216

This consists of two parts: the data section and the decoder. The data section is simply each byte of the encoder prefixed with a 0 (which is the command to push that number). The decoder is:

74              // push stack length
5               // that many times:
0408719      // output a 0
74012172     // bring bottom of stack to the top
700806319    // output this without popping
0172         // swap top two (brings length to top)
0121         // decrement
6               // close loop

7774            // pop counter, push length again
5
74012172     // bring bottom to top
700806319    // output
0172         // bring length to top
0121         // decrement
6               // close loop

# Swift 4, 120 bytes

import Foundation;let q="import Foundation;let q=%c%@%c;print(String(format:q,34,q,34))";print(String(format:q,34,q,34))

Try See it online!

Since this code imports Foundation, and the Swift corelibs can be a little quirky in non-macOS environments, you might not be able to run it (it doesn't work in TIO, or IBM's Swift Sandbox). If, however, you have a macOS environment, then you should be just fine.

# Perl 5, 37 bytes

$_=q{$_=q{0};s/0/$_/;say};s/0/$_/;say

Try it online!

# Javascript REPL, 21 bytes

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

# Foo, 136111 96 bytes

I'm surprised to see that there's no Foo quine here yet!

>&41>&60>&99>&36>&40>&60>&41>&62>&105>&36>&34>&38>&62>&34>&40>&62>&41>&60>&40(<)>(">&"$i>)<($c<)

Try it online!

## Explanation

This is pretty simple as far as quines go. There are two sections: the data and the decoder. The data is encoded on the array, and is decoded as such:

(<)>(">&"$i>)<($c<)

Which basically iterates once over the tape, outputting >& and the integer value of the cell, then iterating once again over the tape, outputting each character. The tape, then, is just the character codes of this decoder.

# Yabasic, 103 bytes

An anonymous Yabasic quine

c$=Chr$(34):q$="c$=Chr$(34):q$=:?Left$(q$,15)+c$+q$+c$+Mid$(q$,16)":?Left$(q$,15)+c$+q$+c$+Mid$(q$,16)

Includes a trailing newline

Try it online!

# Pascal (FPC), 103 bytes

const s=#39'const s=#39;begin write(s[2..12],s,s[10..50])end.'#39;begin write(s[2..12],s,s[10..50])end.

Try it online!

s is the string that the output is generated from. In Pascal, subtrings can be easily extracted with [from..to] syntax. #39 is replacement for ' using its ASCII codepoint. As seen in this program, sequences of character codepoints can be glued together with the rest of the string delimited with 's at any time. s consists of characters before and after 's concatenated together. #39 occurs immediately before first ' and after second ' so it can be put in s only once and used in both substrings in the output.

The version that may be more suitable in modified, quine-like programs is at 106 bytes:

const s='const s=;begin write(s[1..8],#39,s,#39,s[9..52])end.';begin write(s[1..8],#39,s,#39,s[9..52])end.

Try it online!

# !@#$%^&*()_+, 76 bytes 40Kjiiiiiiiiiiij,306j,6641,iK31,i,6j,,!!_+!^!&(@^!&)++!_+%(!_^^^^^^^^^^^_@%) Try it online! The code can be decomposed into two sections: the data and the decoder. The data is this: 40Kjiiiiiiiiiiij,306j,6641,iK31,i,6j,, Each character pushes itself, and corresponds to a command (shifted up by 11). The decoder is this: !!_+!^!&(@^!&)++!_+%(!_^^^^^^^^^^^_@%) This can also be divided into two parts, one which prints the data raw, and one which decodes the data. This part prints the initial data segment: !!_+!^!&(@^!&)++!_+% !!_+ push 0 (duplicate twice then subtract) this is our delineator !^ push 1 (duplicate and increment) this is our pointer !& push the entry at that index in the stack ( ) until the dilineator is found: @ output the stack entry ^ increment pointer !& refresh entry at index ++!_+ pop top two (add twice, duplicate, subtract) % push 0 underneath stack Then the decoder: (!_^^^^^^^^^^^_@%) ( %) For each character: !_ _ subtract ^^^^^^^^^^^ 11 @ and output it # Muriel, 36 bytes A:"\";.\"A:\\\"\"+|A+A";."A:\""+|A+A Try it online! Since Muriel isn't on TIO (yet!), I've included the interpreter in the link. Thanks Dennis! Quines are the base component of any complex Muriel program, since they're a requirement for any sort of loop. ### Explanation: A: Assign to A " ... "; An escaped version of ";."A:\""+|A+A . Print "A:\"" A:" +|A Escaped version of A +A Then A itself # Aubergine, 16 bytes -a1+a1=oA:bA=iB Try it online! The program has a trailing null byte. Works similarly to my hello world. ## Perl 6, 36 printf |(q<printf |(q<%s>xx 2)>xx 2) Based on the Perl 5 quine. # Brachylog v2, 12 bytes "~k;?w₁";?w₁ Try it online! Full program. Essentially a translation of Fatalize's (non-builtin) Brachylog v1 answer, although it also uses different SWI-Prolog formatting sequences, or rather, a single different one, which saves about 20 bytes (both [34:s, both :34]s, and both ~cs). It seems to have existed back in 2016, but it was probably bugged or something. The last two bytes saved come from using the implicit input, which Brachylog being Brachylog is useful even when the program receives no input, because it's a variable (so instead of explicitly unifying the string with S, we just let it be implicitly unified with ?). w Print "~k;?w₁" "~k;?w₁" which is the input ₁ formatted with ;? the input. ~k (so that the ~k is replaced with the input's canonical representation, i.e. in quotes) We don't actually need to use implicit input--"~kgjw₁"gjw₁ works just as well (and might even translate back to v1)--but doing so regardless manages to both more closely mirror the structure of the original and feel cleverer. # Pip, 26 23 bytes Y\"O"Y"ORPyy\"O"Y"ORPyy Try it online! # C (gcc), 85 bytes #define q(k)main(){puts(#k"\nq("#k")");} q(#define q(k)main(){puts(#k"\nq("#k")");}) Try it online! The q() macro expands into a program that prints out its argument on the first line, and prints out the argument called by q() itself in the second line. So: #define q(k)main(){puts(#k"\nq("#k")");} q(foo) would expand into: main(){puts("foo""\nq(""foo"")");} and after string literal concatenation, becomes: main(){puts("foo\nq(foo)");} And executing and running the program would produce: foo q(foo) Replacing foo with the macro definition itself results in the quine. ## C, 353 bytes char q[]={125,59,109,97,105,110,40,41,123,112,114,105,110,116,102,40,34,99,104,97,114,32,113,91,93,61,123,34,41,59,99,104,97,114,42,112,61,113,59,119,104,105,108,101,40,42,112,41,112,114,105,110,116,102,40,34,37,100,44,34,44,42,112,43,43,41,59,112,117,116,115,40,113,41,59,125,};main(){printf("char q[]={");char*p=q;while(*p)printf("%d,",*p++);puts(q);} • With some golfing, including changing it from hexadecimal to decimal, this can be 354 bytes – Jo King Aug 2 '19 at 2:04 • The link itself is a gcc compiler and it works fine. Are you sure they aren't just warnings? – Jo King Aug 2 '19 at 2:51 • If only the output is erroring, then what is the difference between the two programs? What version of gcc are you using? – Jo King Aug 2 '19 at 3:15 • ... What is the difference between the code of the two programs, not their behaviour. I already know that the second one fails. The version TIO uses is 8.3, and that works fine. – Jo King Aug 2 '19 at 3:32 • I don't care about the output of the second generation quine. I would like to know the output of the first program, the one that didn't fail and produced something that did. I don't want to know about the errors that you have commented about several times already. I would like to know the difference between the program I have given you and its output. You can put the program in an online compiler like I have and link it in a comment below. – Jo King Aug 2 '19 at 3:49 # Javascript (REPL), 2322 21 bytes someone else posted this first (_=x=>(_=${_})())()

paste into chrome console or equivalent to test

# JavaScript (V8), 4948 47 bytes

@NieDzejkob saved 1 byte on both versions

console.log((_=x=>console.log((_=${_})()))()) Try it online! # Wren, 287 bytes var a="[118,97,114,32,97,61,34].each{|m|System.write(String.fromCodePoint(m))} System.write(a+String.fromCodePoint(34)+String.fromCodePoint(10)+a)" [118,97,114,32,97,61,34].each{|m|System.write(String.fromCodePoint(m))} System.write(a+String.fromCodePoint(34)+String.fromCodePoint(10)+a) Try it online! ## Explanation var a= // Define the variable a "[118,97,114,32,97,61,34].each{|m|System.write(String.fromCodePoint(m))} System.write(a+String.fromCodePoint(34)+String.fromCodePoint(10)+a)" // As the string that processes the variable // A literal newline is inserted and can be decoded literally. [118,97,114,32,97,61,34].each{|m|System.write(String.fromCodePoint(m))} // Output the string "var a=" to the console System.write( // Output without a newline: a // The string a +String.fromCodePoint(34) // Plus a quote +String.fromCodePoint(10) // Plus a newline +a) // Plus the string again $$$$ • Why doesn't fromByte work in place of fromCodePoint? – Jo King Oct 29 '19 at 23:28 • I have absolutely no idea. – user85052 Oct 30 '19 at 3:57 # Symbolic Raku, 36 bytes$_={$_~"<$_>)"}(<$_={$_~"<$_>)"}(>) Try it online! ### Explanation:$_=                                   # Set the output to
{          }(                  )   # The result of the code block
<                >    # With this string:
$_={$_~"<$_>)"}( # The first half of the program$_~                               # Concatenate this string with
"<\$_>)"                        # The quoted string, and the extra bracket

# Keg, 86 4 bytes

④④

Try it online!

### 8 bytes

:.,:.,

Try it online!

Why did it take me so long to figure out how to write a quine in Keg? I really should have picked up on this sooner.

Basically, it pushes the string :.,, duplicates it, prints the string repr'd and then prints it nicely.

## W, 13 bytes

Print the data string & prepend quote.

p34CS+"p34CS+

## tq, 8 bytes

New high-level language! (Technically inspired by Jo King's Symbolic Raku quine.)

etq'etq'

## Explanation

'etq' # Define the second item of the list
# As a string
q      # Surround the string with quotes
t       # For the first item of the list,
# Access the last (tail) item in the list,
e        # and un-quote the accessed value.

# The list becomes etq, 'etq' (comma is for readability)
# , which then becomes foreach-printed without any separator.
$$$$

# Common Lisp, 58 bytes

Lisp is perfect for quines because of using code as data, but terseness is not its strong suit.

(FORMAT T "(~{~S ~}~:*'~S)" '(FORMAT T "(~{~S ~}~:*'~S)"))

Excellent expert explanation.

(FORMAT T  -- print
"(      )" -- between parentheses
" ~{  }~ " -- looping over the list argument
" ~S_    " -- each item followed by a space
"   ~:*  " -- use the FORMAT sublanguage's *very* fancy
-- "~*" directive (skip argument) with the ":"
-- modifier to back up and reuse the argument
"    '~S " -- print the argument again with a quote before it
'(FO.. -- the argument is the same thing but with a quote
-- in front to show that it is data

Ideone it!

## Alternative 9 bytes, that only works in the REPL

(prin1 -)

(use print for a trailing newline)

This prints the value of '-', which is the current expression being evaluated. You can try it here.

# Perl 5 + -p, 35 bytes

A fishy quine!

}{s<><}{s<><@>;s<@><lc>e>;s<@><lc>e

Try it online!

# Aceto, 66 bytes

£"24«cs%55«3+cp24«2+cdpsdpsppn"24«cs%55«3+cp24«2+cdpsdpsppn

Kind of a classic quine, but having to deal with a few quirks of Aceto to make it work. I had attempted this in the past but had failed.

This mostly follows the good old "source code in quotes, followed by printing a quote character and the source code twice" method.

### Explanation:

£"24«cs%55«3+cp24«2+cdpsdpsppn"24«cs%55«3+cp24«2+cdpsdpsppn
£                                                            # (1)
"24«cs%55«3+cp24«2+cdpsdpsppn"                              # (2)
24«cs%                        # (3)
55«3+cp24«2+cdpsdpsppn  # (4)

Because of the development process of a quine, I decided to ignore the Hilbert curve in this case, because otherwise I'd need to scramble the source code to match the formatting. Therefore we write everything in a single line, exploiting the fact that it will be well-ordered still (just walk over a bunch of nops (spaces) in-between).

1. I just realized getting an empty string is not really trivial in Aceto (oops). Character literals (starting with ') are always 1 character long. You could use string literals, but just doing "" will only work sometimes, depending on where on the hilbert curve you are currently. Since we're trying to avoid the curve for this (as described above), this will usually just turn into a string containing a couple of spaces. But when the stack is empty (such as at the beginning of the execution), we can "implode" (£) the stack into a single string, which will then be the empty string.
2. This is, as usual, just the remaining source code after this, as a string literal.
3. I will need to remove all spaces from the source code that have agglomerated there due to the Hilbert-curve. I can't use a space literal for several reasons (Hilbert curve, and it should be trimmed later), so I instead construct it (and other characters later) using arithmetical operations. An ASCII space is 2, bit-shifted to the left 4 times (24«c). We then use the regex substitution operator % to remove the spaces.
4. Now we're ready to print things: We construct the pound symbol (55«3+c) and print it, followed by a double-quote (24«2+c), followed by the space-trimmed source code, a quote, and the source code again. Finally, we print a newline (n).

# Integral, 18 12 Bytes

⌡^♦►◙►⌡^♦►◙►

Try it not on TIO!

• Can you update this? A resolution is in the issues on the GitHub. – nph Aug 5 at 15:56
• ⌡^♦►◙►⌡^♦►◙► (12 bytes). Same length as your previous 12-byter, but with the "bug" fixed. – petStorm 2 days ago

with Text_IO;use Text_IO;procedure Q is M:Character:='"';T:String:="with Text_IO;use Text_IO;procedure Q is M:Character:=';T:String:=;begin Put_Line(T(1..54)&M&T(54..65)&M&T&M&T(66..126));end Q;";begin Put_Line(T(1..54)&M&T(54..65)&M&T&M&T(66..126));end Q;

pretty printed:

with Text_IO;
use Text_IO;
procedure Quine is
M : Character := '"';
T : String := "with Text_IO;use Text_IO;" &
"procedure Q is M:Character:=';T:String:=;" &
"begin Put_Line(T(1..54)&M&T(54..65)&M&T&M&T(66..126));end Q;";
begin
Put_Line(T(1..54)&M&T(54..65)&M&T&M&T(66..126));
end Quine;

this program actually produces the short version (output can be redirected to q.adb).

Pretty certain you can't get shorter than this with Common Lisp. The first one I managed to figure out myself; all credit to http://rosettacode.org/wiki/Quine#Common_Lisp for the second one.

# Common Lisp - 9 (With REPL)

(write -)

In Common Lisp, - is a special variable that holds the expression currently being evaluated but only during a read-eval-print loop. If you're running from a script or you've disabled the REPL though...

# Common Lisp - 25 (No REPL)

#1=(write '#1# :circle t)

This references itself and then prints itself out. :circle t makes it detect the infinite recursion cycle and exit early.

# CJam, 1 byte

Note: CJam was made after this challenge was posted, I am posting this answer as documentation as opposed to a serious entry.

0

Pushes a 0 to the stack, and CJam automatically outputs the stack after program execution.

• Don't forget newlines. – CalculatorFeline May 28 '17 at 1:39
• One part of the program does not encode another, so this program is invalid – MilkyWay90 Feb 5 '19 at 18:04