# 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

# Bash, 54 52 bytes

-2 thanks to Ørjan Johansen

Try it online!

# Bob, 1221 bytes

c=","; n="\n"; q="\""; s="\\";
v=\[
"c=\",\"; n=\"\\n\"; q=\"\\\"\"; s=\"\\\\\";",
"v=\\[",
"define prtQuote(str) {",
" local j,t,v;",
" stdout.Display(q);",
" for (j=0; j<str.size; j++) {",
"  t = str.Substring(j,1);",
"  if (t==q) { stdout.Display(s); }",
"  if (t==s) { stdout.Display(s); }",
"  stdout.Display(t);",
" }",
" stdout.Display(q);",
"}",
"for(i=0; i<2; i++){ stdout.Display(v[i]); stdout.Display(n); }",
"for(i=0; i<v.size-1; i++){ prtQuote(v[i]); stdout.Display(c); stdout.Display(n); }",
"prtQuote(v[v.size-1]); stdout.Display(n);",
"stdout.Display(v[v.size-1]); stdout.Display(n);",
"for(i=2; i<v.size-1; i++){ stdout.Display(v[i]); stdout.Display(n); }",
"];"
];
define prtQuote(str) {
local j,t,v;
stdout.Display(q);
for (j=0; j<str.size; j++) {
t = str.Substring(j,1);
if (t==q) { stdout.Display(s); }
if (t==s) { stdout.Display(s); }
stdout.Display(t);
}
stdout.Display(q);
}
for(i=0; i<2; i++){ stdout.Display(v[i]); stdout.Display(n); }
for(i=0; i<v.size-1; i++){ prtQuote(v[i]); stdout.Display(c); stdout.Display(n); }
prtQuote(v[v.size-1]); stdout.Display(n);
stdout.Display(v[v.size-1]); stdout.Display(n);
for(i=2; i<v.size-1; i++){ stdout.Display(v[i]); stdout.Display(n); }

source

# RProgN 2, 3 bytes

«Ø

Try it online!

This code does nothing with the first line, then, pushes the function «Ø. As « is unmatched, it falls through and executes the contents. Ø then pushes an empty string. The implicit printing behaviour first prints the empty string, then the function, giving our source code.

# 4 bytes

«•.

Try it online!

This uses the fall through behavoir of the last quine, but ensures "quine" behavoir a different way. pushes a space, . concatenates it, which gives our code.

# 6 bytes

{{.}{

Try it online!

This uses a different fallthrough behavoir. {{.} pushes a funciton, then {, with an unmatched }, fails and moves the IP back to index 1, which skips the function definition, and runs its contents instead. (backtick){ pushes { as a string, then . appends it to the function, stringifying it. } then terminates the program, and the string is implicitely output.

# OML, 20 bytes

"lK(34Tos)"lK(34Tos)

Try it online!

This was an interesting process. I initially started off with the classic data-decoder method of quining. That is, one devotes one section of the program to encoding the decoder, and another section to generate the data representation and decode the data. This came out to look like this (51 bytes):

'\'l'K'l'2'/'['('''''o'o')']'('o')\lKl2/[(''oo)](o)

Where the data section looks like:

'\'l'K'l'2'/'['('''''o'o')']'('o')

which pushes each character after a '. The decoder looks like this:

\lKl2/[(''oo)](o)
\                  reverse stack
lK                duplicate stack
l2/[            copy the duplication into a new stack
(    )      while there are characters on this stack:
''o          output a single quote
o         and the character
(o)  output all characters on this stack

This can be improved slightly by replacing (o) with ls (48 bytes):

'\'l'K'l'2'/'['('''''o'o')']'l's\lKl2/[(''oo)]ls

However, OML also has a length-encoded string construct. Let's say the string is "xyz". This is effectively 'x'y'z3, since there are three characters in the string. We can use s to print this string, but we still need to generate the quote characters. With all this in mind, I was able to devise the following approach (26 bytes):

"lK34Tos34Tos"lK34Tos34Tos

Simply put, this puts the "string" "lK34Tos34Tos" to the stack, then performs the following actions:

lK34Tos34Tos
lk            duplicate the stack (in this case, the string)
34To        output a quotation mark "
s       output the string
34To   output another quotation mark "
s  output the string again

We obtain our final version by noting that the structure 34Tos is repeated twice. We can use a while loop to produce the current answer.

L,"L,%rd%%"d%

Try it online!

Takes advantage of Python string formatting with the % command.

# 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.

# Swift 4, 63 bytes

let s=[";print(\"let s=\\(s)\"+s[0])"];print("let s=\(s)"+s[0])

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!

# Excel, 17 Bytes

In cell A1...

=FunctionText(A1)

I discovered this yesterday by accident. Feels a bit cheaty somehow though. I appreciate this answer much more, but this does seem to be a quine :D

# 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. # Rust, 72 66 bytes fn main(){print!("{}{0:?})}}","fn main(){print!(\"{}{0:?})}}\",")} Try it online! # Java, 515 bytes In order to make this quine, I made strings representing every part of the code that needed to be printed. Then, I created and printed a string representing the code by adding the strings together. This is my first time playing code golf, let me know what you think of my quine! interface a{static void main(String[]a){String ce="interface a{static void main(String[]a){String ",e="=",c="\",",q="\"",cq="\\",ec=";System.out.print(ce+cc+ee+e+q+ce+c+ee+e+q+e+c+cc+e+q+cq+c+c+qq+e+q+cq+q+c+cc+qq+e+q+cq+cq+c+ee+cc+e+q+ec+c+ee+qq+e+q+eq+c+ee+ee+e+q+ee+c+cc+cc+e+q+cc+c+qq+qq+e+q+qq+q+ec+eq",eq=");}}",ee="e",cc="c",qq="q";System.out.print(ce+cc+ee+e+q+ce+c+ee+e+q+e+c+cc+e+q+cq+c+c+qq+e+q+cq+q+c+cc+qq+e+q+cq+cq+c+ee+cc+e+q+ec+c+ee+qq+e+q+eq+c+ee+ee+e+q+ee+c+cc+cc+e+q+cc+c+qq+qq+e+q+qq+q+ec+eq);}} in readable form: interface a { static void main(String[] a) { String ce = "interface a{static void main(String[]a){String ", e = "=", c = "\",", q = "\"", cq = "\\", ec = ";System.out.print(ce+cc+ee+e+q+ce+c+ee+e+q+e+c+cc+e+q+cq+c+c+qq+e+q+cq+q+c+cc+qq+e+q+cq+cq+c+ee+cc+e+q+ec+c+ee+qq+e+q+eq+c+ee+ee+e+q+ee+c+cc+cc+e+q+cc+c+qq+qq+e+q+qq+q+ec+eq", eq = ");}}", ee = "e", cc = "c", qq = "q"; System.out.print(ce+cc+ee+e+q+ce+c+ee+e+q+e+c+cc+e+q+cq+c+c+qq+e+q+cq+q+c+cc+qq+e+q+cq+cq+c+ee+cc+e+q+ec+c+ee+qq+e+q+eq+c+ee+ee+e+q+ee+c+cc+cc+e+q+cc+c+qq+qq+e+q+qq+q+ec+eq); } } • If you want to try shortening this further, have a look into using the printf function – Jo King Jun 28 at 6:41 • I tried that, but I found print easier. I guess I'll try again. – Ethan Gallagher Jul 1 at 2:22 # 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. # 33, 26 bytes "34cktptptptp"34cktptptptp Try it online! Explanation: "34cktptptptp" (The instructions) 34ck (Load 34 (") into destination string) tp (Print it) tp (Print the instructions) tptp (Repeat) # 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! • The first one works in a REPL environment because that outputs the result of each line – Jo King Jul 31 at 23:46 • Save a byte like this: Try it online! – NieDzejkob Aug 1 at 22:50 • Would print((_=x=>print((_=${_})()))()) count too? JavaScript (V8) 35 bytes – pixma140 1 hour ago

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 at 18:04