204
\$\begingroup\$

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;

var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(index){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}
function commentUrl(index,answers){return"https://api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}
function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=!1;comment_page=1;getComments()}})}
function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}
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}
function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))
body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)
valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,})});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)
lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<i>'+a.language+'</i>').text().toLowerCase();languages[lang]=languages[lang]||{lang:a.language,user:a.user,size:a.size,link:a.link,uniq:lang}});var langs=[];for(var lang in languages)
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)
{var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

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

337 Answers 337

2
\$\begingroup\$

x86/DOS assembly, 4619 bytes

bits 16
org 256
mov ah,64
mov bx,1
mov cx,720
mov dx,b+16
int 33
mov cx,90
mov si,b+16
c:push cx
mov ah,64
mov bx,1
mov cx,6
mov dx,b
int 33
mov cx,7
a:push cx
mov al,[si]
shr al,4
add al,48
cmp al,58
jb g
add al,39
g:mov [b+8],al
lodsb
and al,15
add al,48
cmp al,58
jb e
add al,39
e:mov [b+9],al
mov ah,64
mov bx,1
mov cx,5
mov dx,b+6
int 33
pop cx
loop a
mov al,[si]
shr al,4
add al,48
cmp al,58
jb h
add al,39
h:mov [b+8],al
lodsb
and al,15
add al,48
cmp al,58
jb n
add al,39
n:mov [b+9],al
mov ah,64
mov bx,1
mov cx,4
mov dx,b+6
int 33
pop cx
loop c
mov ah,64
mov bx,1
mov cx,5
mov dx,b+11
int 33
mov ah,76
int 33
b   db 13,10,' db 0x  ,',13,10,'end'
 db 0x62,0x69,0x74,0x73,0x20,0x31,0x36,0x0d
 db 0x0a,0x6f,0x72,0x67,0x20,0x32,0x35,0x36
 db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61,0x68
 db 0x2c,0x36,0x34,0x0d,0x0a,0x6d,0x6f,0x76
 db 0x20,0x62,0x78,0x2c,0x31,0x0d,0x0a,0x6d
 db 0x6f,0x76,0x20,0x63,0x78,0x2c,0x37,0x32
 db 0x30,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x64
 db 0x78,0x2c,0x62,0x2b,0x31,0x36,0x0d,0x0a
 db 0x69,0x6e,0x74,0x20,0x33,0x33,0x0d,0x0a
 db 0x6d,0x6f,0x76,0x20,0x63,0x78,0x2c,0x39
 db 0x30,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x73
 db 0x69,0x2c,0x62,0x2b,0x31,0x36,0x0d,0x0a
 db 0x63,0x3a,0x70,0x75,0x73,0x68,0x20,0x63
 db 0x78,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61
 db 0x68,0x2c,0x36,0x34,0x0d,0x0a,0x6d,0x6f
 db 0x76,0x20,0x62,0x78,0x2c,0x31,0x0d,0x0a
 db 0x6d,0x6f,0x76,0x20,0x63,0x78,0x2c,0x36
 db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x64,0x78
 db 0x2c,0x62,0x0d,0x0a,0x69,0x6e,0x74,0x20
 db 0x33,0x33,0x0d,0x0a,0x6d,0x6f,0x76,0x20
 db 0x63,0x78,0x2c,0x37,0x0d,0x0a,0x61,0x3a
 db 0x70,0x75,0x73,0x68,0x20,0x63,0x78,0x0d
 db 0x0a,0x6d,0x6f,0x76,0x20,0x61,0x6c,0x2c
 db 0x5b,0x73,0x69,0x5d,0x0d,0x0a,0x73,0x68
 db 0x72,0x20,0x61,0x6c,0x2c,0x34,0x0d,0x0a
 db 0x61,0x64,0x64,0x20,0x61,0x6c,0x2c,0x34
 db 0x38,0x0d,0x0a,0x63,0x6d,0x70,0x20,0x61
 db 0x6c,0x2c,0x35,0x38,0x0d,0x0a,0x6a,0x62
 db 0x20,0x67,0x0d,0x0a,0x61,0x64,0x64,0x20
 db 0x61,0x6c,0x2c,0x33,0x39,0x0d,0x0a,0x67
 db 0x3a,0x6d,0x6f,0x76,0x20,0x5b,0x62,0x2b
 db 0x38,0x5d,0x2c,0x61,0x6c,0x0d,0x0a,0x6c
 db 0x6f,0x64,0x73,0x62,0x0d,0x0a,0x61,0x6e
 db 0x64,0x20,0x61,0x6c,0x2c,0x31,0x35,0x0d
 db 0x0a,0x61,0x64,0x64,0x20,0x61,0x6c,0x2c
 db 0x34,0x38,0x0d,0x0a,0x63,0x6d,0x70,0x20
 db 0x61,0x6c,0x2c,0x35,0x38,0x0d,0x0a,0x6a
 db 0x62,0x20,0x65,0x0d,0x0a,0x61,0x64,0x64
 db 0x20,0x61,0x6c,0x2c,0x33,0x39,0x0d,0x0a
 db 0x65,0x3a,0x6d,0x6f,0x76,0x20,0x5b,0x62
 db 0x2b,0x39,0x5d,0x2c,0x61,0x6c,0x0d,0x0a
 db 0x6d,0x6f,0x76,0x20,0x61,0x68,0x2c,0x36
 db 0x34,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x62
 db 0x78,0x2c,0x31,0x0d,0x0a,0x6d,0x6f,0x76
 db 0x20,0x63,0x78,0x2c,0x35,0x0d,0x0a,0x6d
 db 0x6f,0x76,0x20,0x64,0x78,0x2c,0x62,0x2b
 db 0x36,0x0d,0x0a,0x69,0x6e,0x74,0x20,0x33
 db 0x33,0x0d,0x0a,0x70,0x6f,0x70,0x20,0x63
 db 0x78,0x0d,0x0a,0x6c,0x6f,0x6f,0x70,0x20
 db 0x61,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61
 db 0x6c,0x2c,0x5b,0x73,0x69,0x5d,0x0d,0x0a
 db 0x73,0x68,0x72,0x20,0x61,0x6c,0x2c,0x34
 db 0x0d,0x0a,0x61,0x64,0x64,0x20,0x61,0x6c
 db 0x2c,0x34,0x38,0x0d,0x0a,0x63,0x6d,0x70
 db 0x20,0x61,0x6c,0x2c,0x35,0x38,0x0d,0x0a
 db 0x6a,0x62,0x20,0x68,0x0d,0x0a,0x61,0x64
 db 0x64,0x20,0x61,0x6c,0x2c,0x33,0x39,0x0d
 db 0x0a,0x68,0x3a,0x6d,0x6f,0x76,0x20,0x5b
 db 0x62,0x2b,0x38,0x5d,0x2c,0x61,0x6c,0x0d
 db 0x0a,0x6c,0x6f,0x64,0x73,0x62,0x0d,0x0a
 db 0x61,0x6e,0x64,0x20,0x61,0x6c,0x2c,0x31
 db 0x35,0x0d,0x0a,0x61,0x64,0x64,0x20,0x61
 db 0x6c,0x2c,0x34,0x38,0x0d,0x0a,0x63,0x6d
 db 0x70,0x20,0x61,0x6c,0x2c,0x35,0x38,0x0d
 db 0x0a,0x6a,0x62,0x20,0x6e,0x0d,0x0a,0x61
 db 0x64,0x64,0x20,0x61,0x6c,0x2c,0x33,0x39
 db 0x0d,0x0a,0x6e,0x3a,0x6d,0x6f,0x76,0x20
 db 0x5b,0x62,0x2b,0x39,0x5d,0x2c,0x61,0x6c
 db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61,0x68
 db 0x2c,0x36,0x34,0x0d,0x0a,0x6d,0x6f,0x76
 db 0x20,0x62,0x78,0x2c,0x31,0x0d,0x0a,0x6d
 db 0x6f,0x76,0x20,0x63,0x78,0x2c,0x34,0x0d
 db 0x0a,0x6d,0x6f,0x76,0x20,0x64,0x78,0x2c
 db 0x62,0x2b,0x36,0x0d,0x0a,0x69,0x6e,0x74
 db 0x20,0x33,0x33,0x0d,0x0a,0x70,0x6f,0x70
 db 0x20,0x63,0x78,0x0d,0x0a,0x6c,0x6f,0x6f
 db 0x70,0x20,0x63,0x0d,0x0a,0x6d,0x6f,0x76
 db 0x20,0x61,0x68,0x2c,0x36,0x34,0x0d,0x0a
 db 0x6d,0x6f,0x76,0x20,0x62,0x78,0x2c,0x31
 db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x63,0x78
 db 0x2c,0x35,0x0d,0x0a,0x6d,0x6f,0x76,0x20
 db 0x64,0x78,0x2c,0x62,0x2b,0x31,0x31,0x0d
 db 0x0a,0x69,0x6e,0x74,0x20,0x33,0x33,0x0d
 db 0x0a,0x6d,0x6f,0x76,0x20,0x61,0x68,0x2c
 db 0x37,0x36,0x0d,0x0a,0x69,0x6e,0x74,0x20
 db 0x33,0x33,0x0d,0x0a,0x62,0x20,0x20,0x20
 db 0x64,0x62,0x20,0x31,0x33,0x2c,0x31,0x30
 db 0x2c,0x27,0x20,0x64,0x62,0x20,0x30,0x78
 db 0x20,0x20,0x2c,0x27,0x2c,0x31,0x33,0x2c
 db 0x31,0x30,0x2c,0x27,0x65,0x6e,0x64,0x27
end
\$\endgroup\$
2
\$\begingroup\$

ACL2, 41 bytes

(let((q"(let((q~x0))(cw q q))"))(cw q q))
\$\endgroup\$
2
\$\begingroup\$

BaCon, 54 bytes

s$="s$=%c%s%c:?34,s$,34 FORMAT s$":?34,s$,34 FORMAT s$
\$\endgroup\$
2
\$\begingroup\$

Excel, 131 bytes

=SUBSTITUTE("=SUBSTITUTE(@,CHAR(64),CHAR(34)&@&CHAR(34))",CHAR(64),CHAR(34)&"=SUBSTITUTE(@,CHAR(64),CHAR(34)&@&CHAR(34))"&CHAR(34))

Adapted from a program by Dave Burt.

\$\endgroup\$
2
\$\begingroup\$

Go, 112 bytes

As far as I can tell, there's no Go answer here. Here's mine and I think this is the shortest possible.

package main;import.`fmt`;func main(){s:="package main;import.`fmt`;func main(){s:=%q;Printf(s,s)}";Printf(s,s)}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

QBIC, 8 bytes

?A+@?A+@

I was trying to do this, but I accidentally golfed an actual quine. Whadda you know?

Explanation

?           Print
 A            A$
  +         concatenated with
   @       a string literal containing
    ?A+@   "?A+@"

When A$ is used the first time, it might not seem to have a value yet, but it already contains the string literal ?A+@ because the QBIC interpreter first scans the code, sees the @, looks up what the first available string variable is (it's A$, because it hasn't been used by other QBIC language features yet), and it then extracts the definition A$ = "?A+@" to the top of the file, and inserts A$ at the place where it found the literal.

\$\endgroup\$
2
\$\begingroup\$

Micro, 5 bytes

{_BS}

Explination:

{_BS}

{      start a code block (this block will be run due to implicit evaluation)
 _     push the item most recently popped. this pushes the code block which was popped due to implicit evaluation
  BS}  convert the code block to a string, and end the block. Micro's implicit evaluation is weird, because the evaluated variable may generate new items on the stack, in which case the program's execution will continue as if it hadn't ended. this second implicit evaluation displays the string which is left on the stack: "{_BS}", and ends execution.
\$\endgroup\$
2
\$\begingroup\$

Mathcad, 94 characters

f:(c{"f:(c{"")]concat(substr(c,0,6),c,substr(c,6,41))")]concat(substr(c,0,6),c,substr(c,6,41))

Watch out for quotation marks autocompletion in the editor!

This should create a parameterless function f which returns its own code.

This quine makes use of (another) weird feature of Mathcad: you can put as much quotation marks inside a string as you like. No idea how they handle it...

\$\endgroup\$
2
\$\begingroup\$

JScript, 175 bytes

WScript.Echo((k="WScript.Echo((k=\"%Z\").replace(/%[Z]/,k.replace(/[\"\\\\]/g,function(e){return\"\\\\\"+e})))").replace(/%[Z]/,k.replace(/["\\]/g,function(e){return"\\"+e})))

JScript is Microsoft's implementation of the JavaScript language. On a microsoft console, you can invoke the program as <name>.js, and this will output to a popup. To output to the console, one must use:

cscript //E:JScript //nologo <name>.js

and add a trailing CRLF to the source code.

\$\endgroup\$
2
\$\begingroup\$

Foam, 15 bytes

[. <' |: ~|]: ~

This prints itself with a trailing newline. Without a trailing newline:

[. .' |: ~|]: ~
\$\endgroup\$
2
\$\begingroup\$

Gaia, 10 bytes

“:ṙpp”:ṙpp

Try it online!

Explanation

“:ṙpp”      Push this string.
      :     Copy it.
       ṙ    Get the string representation of it.
        p   Print the string representation.
         p  Print the string.
\$\endgroup\$
2
\$\begingroup\$

PHP, 50 chars

<?printf($s='<?printf($s=%c%s%1$c,39,$s);',39,$s);

I realized I could save four chars by enabling short tags (<?=sprintf becomes <?printf)

I saved four more bytes by reusing one of the values in my printf.

\$\endgroup\$
2
\$\begingroup\$

Proton, 32 30 bytes

-2 bytes thanks to ppperry.

s='s=%rprint(s%%s)'print(s%s)

Try it online!

I swear, if I get a bounty for this...

\$\endgroup\$
  • \$\begingroup\$ Hey, I promised a bounty, so if nobody beats it, good for you I guess :P \$\endgroup\$ – HyperNeutrino Aug 17 '17 at 19:39
  • 1
    \$\begingroup\$ You can save two bytes by deleting the semicolons \$\endgroup\$ – pppery Aug 18 '17 at 22:18
2
\$\begingroup\$

C (tcc), 64 bytes

main(t){printf(t,34,t="main(t){printf(t,34,t=%c%s%c,34);}",34);}

Try it online!

could be one problem if compiler not use the stack based way of push arguments.
result of the print (tcc, gcc all in TIO today at last ok (not ok in clang)):

main(t){printf(t,34,t="main(t){printf(t,34,t=%c%s%c,34);}",34);}
\$\endgroup\$
  • \$\begingroup\$ Related \$\endgroup\$ – No one Dec 23 '17 at 19:09
2
\$\begingroup\$

V, 4 bytes

2i2i

Try it online!

Marked as non-competing because this language postdates the challenge by several years.

Explanation:

2       " Two times:
 i      " Insert the following:
  2i    " The string '2i'

This is pretty straightforward. In fact, this almost works in regular vim. There is just one minor thing in the way: The string '2i' isn't inserted twice until you hit <esc>. In V, this is solved by implicitly ending every program with an <esc> (Really, that's an oversimplification, but it's close enough to the truth).

Old versions of V always added a newline to the output, which is why I didn't post this earlier. However, in commit b6c238d, this was fixed.

This answer works just because of luck. The approach doesn't extend well to general purpose quines/quine-variations. The shortest quine I'm aware of that can be trivially modified is

ñéÑ~"qpÿ

Try it online!

Explanation:

ñ           " Start recording into register 'q'
 éÑ         " Insert 'Ñ' (uppercase so that the recording doesn't stop here)
   ~        " Toggle the case the character under the cursor (the 'Ñ')
    "qp     " Paste the contents of register 'q'
       ÿ    " Stop recording and play it all back

The reason the ÿ is needed, is because it is implicitly added to the end of macros, a feature that is unfortunate for quines, but very convenient for golf.

The nice thing about this quine is that we can do almost anything inside of the recording and it is still valid since it will be pasted later anyway.

\$\endgroup\$
2
\$\begingroup\$

MaybeLater, 74 bytes

x="write(('x='+chr(34))+x+(chr(34))+x)"write(('x='+chr(34))+x+(chr(34))+x)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

ABAP, 515 bytes

REPORT R NO STANDARD PAGE HEADING.DATA:A TYPE TABLE OF STRING,B(8).APPEND:
`REPORT.FORM F TABLES T.NEW-PAGE LINE-SIZE 78.WRITE:'REPORT R NO',` TO A,
`'STANDARD PAGE HEADING.DATA:A TYPE TABLE OF STRING,B(8).APPEND:'.LOOP` TO A,
`AT T.REPLACE ALL OCCURENCES OF'``' IN T WITH'````'.WRITE:/'``'` TO A,
`NO-GAP,T NO-GAP,'`` TO A,'.ENDLOOP.WRITE:AT 78'.','GENERATE',` TO A,
`'SUBROUTINE POOL A NAME B.PERFORM F IN PROGRAM (B) TABLES A.'.ENDFORM.` TO A.
GENERATE SUBROUTINE POOL A NAME B.PERFORM F IN PROGRAM (B) TABLES A.

Should work on on any SAP system with SY-SAPRL >= '700'.

source

\$\endgroup\$
  • \$\begingroup\$ is this an error quine? \$\endgroup\$ – Destructible Lemon May 24 '17 at 23:53
  • \$\begingroup\$ @DestructibleLemon No, why do you ask? \$\endgroup\$ – MD XF May 25 '17 at 0:46
  • \$\begingroup\$ Anyone care to explain the serial downvoting? \$\endgroup\$ – MD XF May 25 '17 at 17:20
2
\$\begingroup\$

Java 8 - 392 bytes

interface Q{static void main(String[]a){p("interface Q{static void main(String[]a){p(");q(");}static void p(String s){System.out.print(s+(char)34+s+(char)34+')'+';'+'q'+'('+(char)34);}static void q(String s){System.out.print(s+(char)34+s);}}");}static void p(String s){System.out.print(s+(char)34+s+(char)34+')'+';'+'q'+'('+(char)34);}static void q(String s){System.out.print(s+(char)34+s);}}

The main trick with this was using 34 cast to a character for the quotes that bound the string literals in order to not run into issues.

\$\endgroup\$
  • 3
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Steadybox Jan 9 '18 at 15:53
2
\$\begingroup\$

Wumpus, 9 bytes

"#34#9&o@

Try it online!

Explanation

This is a fairly standard Fungeoid quine. However, as opposed to most other Fungeoids, Wumpus's grid doesn't wrap around, so the IP actually bounces back and forth through the code:

"#34#9&o@o&9#32#"
     This pushes the individual code points of this string to the stack.
#34  Push 34.
#9   Push 9.
&o   Print 9 characters from the top of the stack.
@    Terminate the program.

There are several other ways to do this in 9 bytes, e.g. this one which generates the " from the # instead:

"#9[(~&o@

I haven't yet found a way to get it down to 8 bytes though (it might be possible: if there's a way to generate the 34 in three bytes that doesn't end in a digit, we could get rid of the # in front of the 9).

\$\endgroup\$
2
\$\begingroup\$

Rust, 108 characters

macro_rules!f(()=>("macro_rules!f(()=>({:?}));fn main(){{print!(f!(),f!())}}"));fn main(){print!(f!(),f!())}

This is a suboptimal solution, but it's so close to the current shortest solution that I wanted to post it anyway as it uses a completely different strategy. I think it can be optimized by using macro keyword instead of verbose macro_rules! when it becomes stable (which would reduce this to 96 characters).

\$\endgroup\$
2
\$\begingroup\$

Pari/GP, 29 bytes

(f=()->print1("(f="f")()"))()

Try it online!

\$\endgroup\$
2
\$\begingroup\$

SmileBASIC, 66 54 bytes

?MID$(("+CHR$(34))*3,19,54)?MID$(("+CHR$(34))*3,19,54)

This will work in most BASIC dialects.

\$\endgroup\$
2
\$\begingroup\$

Nim, 95 bytes

import strutils;let s="import strutils;let s=$#;echo s%s.repr[^49..^1]";echo s%s.repr[^49..^1]

Try it online!

After scrolling through the leaderboard snippet, I was surprised to see that Nim hasn't been represented here yet. So, let's fix it!

This follows the standard pattern of constructing and printing a string with a quoted representation of itself inserted in the middle. Unfortunately, Nim has a few features that make this golf-unfriendly:

  • String interpolation with % is not available unless you import strutils.
  • repr not only quotes strings, but also prepends them with a hex number (seemingly, memory address), like this: 0x40a6d0"my string". Therefore, we have to extract the right part.

Still, not so bad overall, as it is significantly shorter than the version currently posted at Rosetta Code, which actually doesn't even work without tweaks in recent versions of the language.

\$\endgroup\$
  • \$\begingroup\$ var x="var x=;echo x[0..5],x.repr[14..^1],x[6..^1]";echo x[0..5],x.repr[14..^1],x[6..^1] \$\endgroup\$ – ASCII-only Aug 27 at 3:25
2
\$\begingroup\$

Gol><>, 6 5 bytes

sP#H"

Try it online!

Credit to Jo King.

How it works

sP#H"

s      +16
 P     +1
  #    Reverse direction
 P     +1
s      +16
    "  Start string literal
sP#H"  Push H, #, P, s and end string literal
   H   Print everything on the stack from the top, and halt
       The printed chars are s, P, #, H, 34 (")

Previous solution, 6 bytes

"r2ssH

Try it online!

How it works

"r2ssH  Push the string "r2ssH" to stack, "r" being at the bottom
"       Close the literal
 r      Reverse the stack
  2ss   Push 34 (")
     H  Print all content of the stack from top to bottom as chars, and halt

There were a couple of alternatives to consider:

  • S" prints the string right away (instead of pushing to stack), but then it gets harder to handle ".
  • `" is an alternative way to push 34 to the stack, but the string literal also treats `" as escaped " which is not desirable.
\$\endgroup\$
  • \$\begingroup\$ An interesting 8 byter using S" \$\endgroup\$ – Jo King Nov 28 '18 at 0:34
2
\$\begingroup\$

Cardinal, 10 bytes

",-#) %8-$

Try it online!

This should be a quine, except for a small bug in the interpreter which causes the ( command to throw an error. For completeness' sake, here's a version where a # is placed in the position the ( is accidentally calling. Note that the space is actually a NUL character. Bug is fixed, yay!

"% (#-,0-$ also works with just one pointer.

How It Works:

The % creates two pointers, going left and right (the ones going vertical don't matter). The right pointer is delayed by the 8 for three steps, which lets the left pointer execute ,-#) which changes the active value to #, decrements it and prints the ". Then the right pointer starts again, decrementing the active value to -1. $ sets the pointer location to 0,-1, which then runs " over the rest of the code, printing it.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can download fixed and recompiled version (including source code) of the interpreter from my Github repo: github.com/m-lohmann/Cardinal. The original interpreter had several errors that needed fixing. \$\endgroup\$ – M L Jun 2 '18 at 0:08
  • \$\begingroup\$ I don't think your last edit worked properly, the URLs are identical. \$\endgroup\$ – Ørjan Johansen Sep 5 '18 at 18:11
  • \$\begingroup\$ @Orjan lol, it seems I had already made the exact change in a previous edit. \$\endgroup\$ – Jo King Sep 7 '18 at 13:30
2
\$\begingroup\$

J, 27 bytes

echo(,quote)'echo(,quote)'

Try it online!

It's surprising that there's no proper J quine submission yet.

How it works

echo(,quote)'echo(,quote)'
            'echo(,quote)'  The string `s`
      quote                 Wrap `s` with single quotes
     ,                      Concat s to the above; `(f g)x` is equal to `x f g x`
echo                        Explicitly print the result

echo is needed because the result of a non-REPL line is not printed.

\$\endgroup\$
2
\$\begingroup\$

Backhand, 21 bytes

"#v{<@^:[ba+0v|{$:o[}

Try it online!

This is my new 1D language Backhand. It's a little bit more complicated than the typical wrapping string literal quine.

Explanation:

The program initially starts with a step count of 3.

"      Start string literal, stepping 3 places at a time
       This pushes the source code, but all jumbled up :(

See my Hello, World! answer to see what you have to do to push a string normally.

"      End the garbage string literal
  v{   Step left and decrease the step count to 2
    <  Change direction to left
  v    Decrease the step count to 1
 #     No-op
"      Start string literal
       Now the step count is 1, so it actually pushes the source code
 #v{<@^:[ba+0v|{$:o[}    Push to stack going right and bounce off the end
 #v{<@^:[ba+0v|{$:o[     Push to stack going left
"      End string literal
  v    Decrease the step count to 0
  v    Decrease the step count to -1
       Now the pointer is technically going right, but with a step count of -1
"      Push the source code again...
  v    Decrease the step count to -2
    <  Change direction to left (step count is still negative, so it goes right)

      ^           Increase step count to -1
       :[         Dupe the top of stack (# 35) and decrement to 34 (")
         ba+      Add 10 and 11 to push 21 as the counter
            0     Push 0
             v    Decrease step count to -2
               {  Step left
              |   Pop 0 and continue moving left
                $ o    Swap the top two items and print the character
                   [}  Step right (against the wall so it bounces) and decrement the counter
              |{ :     Duplicate the counter and reflect if it is non-zero
                       Repeat this 21 times to print the source code
        [ a 0  Garbage
      ^        Increase the step counter to -1
     @         Terminate program
\$\endgroup\$
2
\$\begingroup\$

Elixir, 44 bytes

q=:'q=:~p;:io.format q,[q]';:io.format q,[q]

Try it online!

This is basically an existing quine taken from here, but I managed to save another 2 bytes by declaring q as an atom instead of a binary.

\$\endgroup\$
2
\$\begingroup\$

Clean, 123 102 bytes

module q;import StdEnv;Start=(s,q,s,q);q=inc'!';s="module q;import StdEnv;Start=(s,q,s,q);q=inc'!';s="

Save as q.icl and compile with -b -nt.

Saved 21 bytes thanks to Οurous.

\$\endgroup\$
  • \$\begingroup\$ This can be improved to 102: module q;import StdEnv;Start=(s,q,s,q);q=inc'!';s="module q;import StdEnv;Start=(s,q,s,q);q=inc'!';s=" \$\endgroup\$ – Οurous Feb 13 at 7:09
  • \$\begingroup\$ @Οurous nice idea, thanks. \$\endgroup\$ – user42682 Feb 13 at 8:36
2
\$\begingroup\$

Rust, 72 66 bytes

fn main(){print!("{}{0:?})}}","fn main(){print!(\"{}{0:?})}}\",")}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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