246
\$\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\$
3
  • 6
    \$\begingroup\$ Do you not mean, "Golf you a quine for greater good!"? \$\endgroup\$ Commented May 3, 2011 at 2:49
  • 64
    \$\begingroup\$ @muntoo it's a play on "Learn you a Haskell for Great Good". \$\endgroup\$ Commented May 3, 2011 at 2:52
  • 28
    \$\begingroup\$ Did anybody notice that this is question 69? \$\endgroup\$
    – aidan0626
    Commented Oct 24, 2020 at 22:47

460 Answers 460

1
6 7
8
9 10
16
3
\$\begingroup\$

R, 73 bytes (incl. tabs and newlines)

(f=function()
{
    cat("(f=function()\n")
    print(body(f))
    cat(")()")
}
)()

Shorter, less interesting quines are possible: function()1 is 11 bytes. + (1 byte) is an arguable quine, since the command prompt for an unfinished line is a plus sign by default, and + on its own counts as an unfinished line.

\$\endgroup\$
5
  • \$\begingroup\$ It is necessary to format exactly this way (can't remove tabs or replace newline with ";"), otherwise the output will not exactly match the input. \$\endgroup\$
    – JDL
    Commented Aug 25, 2016 at 14:17
  • \$\begingroup\$ A more trivial example is function()1 (11 bytes) but that's kinda boring... \$\endgroup\$
    – JDL
    Commented Aug 26, 2016 at 14:06
  • 2
    \$\begingroup\$ Your 14 bytes solution is not easy to find. And I don't find it boring at all! Suggestion: keep the 73 solution but list this solution as 14 bytes one so it found more easily using the leaderboard. \$\endgroup\$
    – JayCe
    Commented May 16, 2018 at 17:56
  • \$\begingroup\$ I may be misreading some details of how programs are defined in R, but I think list() is a shorter trivial solution? I can't do command-line verification, but it certainly plays interactively and on TIO (though you might argue a newline is needed for the latter) Try it online! \$\endgroup\$ Commented Jan 31, 2019 at 15:37
  • \$\begingroup\$ yes, list() (and numeric(0) and friends) are all quines in R. \$\endgroup\$
    – JDL
    Commented Feb 1, 2019 at 8:57
3
\$\begingroup\$

Julia, 36 bytes

(~=:@printf "(~=:%s)|>eval" ~)|>eval

Try it online!

Background

Unlike many other languages, Julia's eval doesn't work as expected with a string; for example, eval("print(42)") just returns the string print(42).

To actually executed print(42) with eval;, we have to pass an Expr to eval. This can be done by invoking parse on a string (e.g., eval(parse("print(42)"))) or by passing an Expr literal to eval (e.g., eval(:(print(42))).

Now, while : is a unary operator and :print works fine on its own, :print(42) does not, as it is parsed as (:print)(42), making all parentheses in :(print(42)) mandatory.

However, if we use the macro @printf instead, the parsing rules change, and :@printf(42) works as intended. Also, macro calls also do not require parentheses, and :@printf 42 saves one byte over :(print(42)).

How it works

:@printf "(~=:%s)|>eval" ~ constructs the Expr that calls @printf with the specified format string and additional argument ~. Here, ~ is simply a variable reference; the name is arbitrary.

~=<Expr> saves the generated Expr in the variable ~, which will be accessible when the Expr is evaluated.

Finally, (<assigment>)|>eval calls eval with the return value of the assignment, i.e., the Expr that was assigned to ~.

\$\endgroup\$
3
\$\begingroup\$

Y, 2 bytes

Try it here!

Up

This is two commands. U is a capture link, and begins quoting the code, and has a U at the beginning of the result. It wraps around, since there is no matching U, and captures the string Up. Then, p prints it, and we are done.

\$\endgroup\$
3
\$\begingroup\$

C#, 188 157 149 bytes

class A{static void Main(){var a="class A{{static void Main(){{var a={0}{1}{0};System.Console.Write(a,'{0}',a);}}}}";System.Con‌​sole.Write(a,'"',a);‌​}}

Basic quine, just contains a self-containing string.

\$\endgroup\$
1
  • \$\begingroup\$ More shorter version (149 bytes): class A{static void Main(){var a="class A{{static void Main(){{var a={0}{1}{0};System.Console.Write(a,'{0}',a);}}}}";System.Console.Write(a,'"',a);}} \$\endgroup\$ Commented Sep 15, 2016 at 11:19
3
\$\begingroup\$

PowerShell, 41 37 Bytes:

function q{"function q{$function:q};q"};q

filter q{"filter q{$function:q};q"};q

Thanks to TimmyD for saving 4 bytes

\$\endgroup\$
4
  • \$\begingroup\$ What interpreter / compiler does this work in? In this interpreter, this submission doesn't work (it outputs function q{End: { "function q{$function:q};q" }};q). \$\endgroup\$
    – xenia
    Commented Oct 6, 2016 at 18:00
  • \$\begingroup\$ @Loovjo Most online PowerShell interpreters use an open-source PoSH that's roughly equivalent to PowerShell v0.5 and lacking many features. The above works fine in an actual install on Windows. \$\endgroup\$ Commented Oct 7, 2016 at 13:08
  • \$\begingroup\$ At least in v4 on Windows 8.1, you can shave a few bytes using filter as follows -- filter q{"filter q{$function:q};q"};q for 37. \$\endgroup\$ Commented Oct 7, 2016 at 13:09
  • \$\begingroup\$ $MyInvocation.MyCommand.ScriptBlock is 2 bytes shorter but almost definitely cheating? \$\endgroup\$
    – colsw
    Commented Feb 22, 2017 at 22:38
3
\$\begingroup\$

Haystack, 7 bytes

Yay, my first quine!

"34c,o|

Try it online!

Explanation

This is a standard 2D quine.

"           starts to push a string
34c,o|      part of the string
"           it wraps around and go to the beginning of this line thus pushing the string
34          push this number
c           output as character (ie outputs ")
,           discard this value
o           output the top of stack (ie 34c,o|)
|           end program
\$\endgroup\$
3
\$\begingroup\$

Actually, 4 bytes

0
0

Note the trailing linefeed. Try it online!

This exploits a potential flaw in our definition of proper quine:

It must be possible to identify a section of the program which encodes a different part of the program. ("Different" meaning that the two parts appear in different positions.)

Furthermore, a quine must not access its own source, directly or indirectly.

The stack of Actually is printed backwards, so the first 0 encodes the second 0, and vice versa.

This can be verified empirically; the program

1
2

prints

2
1

Try it online!

\$\endgroup\$
0
3
+50
\$\begingroup\$

Logicode, 1368 1241 1096 1086 bytes

var a=000101011000111101001111001011000110100000111000001010001100101010100101011010111110110001111011111101110110010001000001100101010110101111101000000010100011001010111100010101111001010111110011110001010111100101011111001111100111100010101111001010111110011111001111100111100010101111001010111110011111001111100111110011110001010111100101011111001111100111110011111001111100111100010101111001010111110011111001111100111110011111001111100111100010100101010111110000010100011001010111110011111001111100111110011111001111100111110010100101011111100101000101011011111110101111010001000001110000010100001100010110001011000101100000110001011000101100000110001011000101100000110000011000001100000110001011000101100010110001011000001100000110001011000001100000110001011000001100000110000011000001100000110001011000101100000110000011000001100000110001011000001100010110001011000101100010110000011000101010010101011110000101010111110000010100011000010101001
circ p(e)->cond e->@(e<+e><+e>><+e>>><+e>>>><+e>>>>><+e>>>>>><)+p(e>>>>>>>)/e
out p(111011011000011110010010000011000010111101)+a+p(a)

Try it online

Explanation

This is pretty simple as far as Quines go. The first line of the program assigns a very long list of ones and zeros to a variable called a this is the binary representation of the last two lines of the program with each character represented by 7 bits.

Then I define a function that takes in a binary string and returns it as a ASCII string.

This works pretty simply:

circ p(e)->                                  #Function header
cond e->                                     #If e is non empty
@(e<+e><+e>><+e>>><+e>>>><+e>>>>><+e>>>>>><) #return the ASCII character made by the first 7 bit
+                                            #plus
p(e>>>>>>>)                                  #p of the rest of the string
/                                            #otherwise
e                                            #return e (i.e. the empty string)

Then on the last line we print var a= the binary string and the ASCII representation of the binary string.

\$\endgroup\$
3
\$\begingroup\$

Threead, 24 bytes

">34co<o>o<o">34co<o>o<o

Try it online!

I didn't think to do it like this until I saw Riley's answer. I have no intention to self-award the bounty, so this is non-competitive for it.

Explanation

">34co<o>o<o">34co<o>o<o
">34co<o>o<o"               # Encodes the right half of the program as a string, in the 1st buffer.
             >              # Move to the 2nd buffer.
              34c           # Put the string represented by ascii 34 (") in the second buffer.
                 o          # Write it to STDOUT
                  <o        # Move to the 1st buffer, Write the contents of the string to STDOUT.
                    >o      # Move back to the 2nd Buffer, write it.
                      <o    # Move back to the 1st Buffer, write it.

Originally... 129 Bytes...

My plan was to use:

"\x0E\x0E\x0E78g\x0EBv$/s@$@c8$$$$$vB\x0Ep$/c6Bb_$f$vgs@$/Bba\x0E$$$c5$$$$$1c5$$$$p"

34c
>r +o< <_4     r>
l +_2>^[ b rco< +>^]
   _1     -_1    l

where \x0E is the literal SOH.

Try it online!

The string is simply all the commands after it, but with a byte value 4 higher. This was because I can't store a " or a \ in the string, without it getting meta. The rest of the script, acts kind of like my other solution, however manually iterates through the string, printing each character -4.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ "I have no intention to self-award the bounty." You can't. \$\endgroup\$ Commented Jan 13, 2017 at 9:10
  • 4
    \$\begingroup\$ The README on github doesn't seem to mention that you can push string literals. \$\endgroup\$
    – Riley
    Commented Jan 13, 2017 at 14:58
  • \$\begingroup\$ @Riley True, but you can figure that out by looking at the Threead "Hello, World!" program. \$\endgroup\$
    – mbomb007
    Commented Jan 13, 2017 at 21:56
  • \$\begingroup\$ Question, how does string multiplication work? I was trying to get it to work on TIO (to solve your bounty), and I couldn't figure it out. \$\endgroup\$
    – mbomb007
    Commented Jan 13, 2017 at 22:00
  • 1
    \$\begingroup\$ @mbomb007 Hopefully this will help you out. And Riley sorry about that, the readme was kind of poorly written. \$\endgroup\$
    – ATaco
    Commented Jan 14, 2017 at 4:41
3
\$\begingroup\$

Befunge-93, 17 bytes

Thanks to James Holderness for pointing out that this relies on nonstandard interpreter behavior

Slightly late to the party, but here goes!

<@,*2+98_,#! #:<"

Try it here, but you have to copy-paste the code. The program relies on nonstandard interpreter behavior, so it'll print a bunch of leading spaces on TIO. Oops. My bad.

<: sets the program direction to "left"; instruction pointer wraps around

": toggles string mode (pushes ascii value of every character until next ", which it encounters only when it wraps around)

(at this point, it pushes every character to the stack & wraps around. This is where it relies on nonstandard behavior - TIO and the reference interpreter would push a bunch of spaces to the stack)

:<: sets the instruction pointer direction to "left" and duplicates top of stack

! #: negates the value at the top (important because of the upcoming _); # skips the next character

_,#: checks the value at the top of the stack & pops it: prints the ascii value as a character of the new top and sets the direction of the instruction pointer to right if checked character was 0; else sets the direction of the instruction pointer to left

@,*2+98: prints the " at the end of the program and quits.

\$\endgroup\$
0
3
\$\begingroup\$

Threead, 101 bytes

>91>60>93>62>91>105>54>50>99>111>100>111>62>93>60>91>60>93>62>91>99>111>62>93[<]>[i62codo>]<[<]>[co>]

Try it online!

My first thought for writing a Threead quine was to store the entire data section as one large number, in order to get a good compression ratio. This doesn't work because a) % appears to be broken, and b) Threead doesn't support bignum arithmetic.

Instead, I wrote this solution, which works along the same lines as a brainfuck quine, storing the characters of the code section of the program as individual tape elements. Then we just have to scan the list once in order to print it as data, and again to print it as code.

Although Threead allows for three threads, and requires their use when performing binary operations, this style of quine uses only unary operations and thus there was no point in using more than one thread, so I just did everything inside the first.

Explanation

>91>60>…>62>93            ASCII character codes of the rest of the program
[<]>                      Return the pointer to the start of the data
[                         While the current data cell is nonzero:
 i62                        Place 62 (ASCII code of >) on a temporary tape cell
 co                         Output it as a character (i.e. >)
 d                          Delete the temporary tape cell
 o                          Output the current data element as an integer
>]                        then continue the loop with the next data cell
<[<]>                     Return the pointer to the start of the data
[                         While the current data cell is nonzero:
 co                         Output it as a character
>]                        then continue the loop with the next data cell
\$\endgroup\$
3
  • \$\begingroup\$ Nice. Basically the same as mine but with > at the beginning. I like it \$\endgroup\$
    – Riley
    Commented Jan 16, 2017 at 23:01
  • \$\begingroup\$ I hope you don't mind that I used that trick in my new version :) \$\endgroup\$
    – Riley
    Commented Jan 16, 2017 at 23:23
  • \$\begingroup\$ @Riley: That's OK, we're pretty much all cooperating to improve the quine at this point. \$\endgroup\$
    – user62131
    Commented Jan 16, 2017 at 23:25
3
\$\begingroup\$

Alice, 9 bytes

Credits to Sp3000 for the idea of including the !.

"!<@o&9h.

Try it online!

Explanation

This works much like quines in other Fungeoids with an unmatched " that wraps the entire code (except itself) in a string because the instruction pointer move cyclically through the code.

"!<@o&9h."   Push code points of the entire program except the " to the
             stack (irrelevant).
!            Store 46 (the code point of '.') on the tape (irrelevant).
<            Send IP back west.
!            Store 104 (the code point of 'h') on the tape (irrelevant).
".h9&o@<!"   Push code points of the entire program except the " to the
             stack in reverse.
.            Duplicate the 33 (the code point of '!').
h            Increment to 34 (the code point of '"').
             Now the top nine values on the stack correspond to the entire
             code in reverse order.
9&           Repeat the next command 9 times.
o            Print 9 characters from the top of the stack.
@            Terminate the program.
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 24 bytes, 21 characters

.($s={".(`$s={$s})"})

I'm going to attempt to explain this, but be forewarned that I'm terrible at explaining myself.

This code sets $s to the following:

".(`$s={$s})"

This recursively sets the variable $s (the first $s is a plain string, but the second $s is the variable $s) in itself and then the block inside parentheses echoes $s, which at time of execution will be the following:

.($s={".(`$s={$s})"})`
\$\endgroup\$
3
\$\begingroup\$

dc, 16 bytes

[91Pn6120568P]dx

Try it online!

Nothing fancy, posting for completeness.

Explanation

[91Pn6120568P]dx
 91P              # Print "["
    n             # Print the macro
     6120568P     # Print "]dx" encoded as a number
[            ]dx  # Run macro on its own code
\$\endgroup\$
3
\$\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\$
3
\$\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\$
1
  • \$\begingroup\$ 80 bytes by turning it into a called function. \$\endgroup\$
    – bigyihsuan
    Commented Nov 2, 2022 at 19:12
3
\$\begingroup\$

Kotlin, 121 bytes

Nobody cared enough about Kotlin to post it so...

fun main(a:Array<String>){val s="fun main(a:Array<String>){val s=%c%s%1$1c;print(s.format(34,s))}";print(s.format(34,s))}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Groovy, 48 bytes

The language's pretty groovy too.

s="s=%c%s%c;printf(s,34,s,34)";printf(s,34,s,34)

Try it online!

\$\endgroup\$
3
\$\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\$
3
\$\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\$
3
\$\begingroup\$

Zsh, 17 bytes

sed p<<a
sed p<<a
\$\endgroup\$
3
\$\begingroup\$

Octave, 28 bytes

Note: This doesn't work on TIO. I guess it's because TIO doesn't store the command history. It works on the desktop version. I tried it in Octave 4.2.1.


printf('%s',[history(1){:}])

So, what's going on here?

history is a function that can be used to read and manipulate the command history.

history(n) shows the n most recent commands you have typed, numbered:

>> x = 1 + 2;
>> y = x * 3;
>> history(3)
    7 x = 1 + 2;
    8 y = x * 3;
    9 history(3)

As you can see, x = 1 + 2 was the seventh command that was typed after the history was cleared the last time. The command history(3) is included in this list.

Now, history(1) is not a quine, since it gives:

>> history(1)
   10 history(1)

However, if you assign the output from history(1) to an output, you'll get:

>> x = history(1)
x =
{
  [1,1] = x = history(1)
}

It's still not a quine, but it's something we can work with.

Unwrapping this, and we're a bit closer:

>> [history(1){:}]
ans = [history(1){:}]

Notice that the entire command, including brackets are outputted.

Finally, if we print this as a string, using printf, we get:

>> printf('%s',[history(1){:}])
printf('%s',[history(1){:}])

Note: disp([history(1){:}]) almost works, but it appends a trailing newline.

\$\endgroup\$
1
  • \$\begingroup\$ Wouldn't disp([history(1){:}])\n work for 27 bytes? \$\endgroup\$ Commented Aug 29, 2017 at 14:47
3
\$\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\$
1
  • \$\begingroup\$ Related \$\endgroup\$
    – No one
    Commented Dec 23, 2017 at 19:09
3
\$\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\$
0
3
\$\begingroup\$

DipDup, 6 bytes

[_:]_:

Try it online!

Explanation

[_:]        push this list
    _       duplicate
     :      cons
\$\endgroup\$
3
\$\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\$
1
  • \$\begingroup\$ An interesting 8 byter using S" \$\endgroup\$
    – Jo King
    Commented Nov 28, 2018 at 0:34
3
\$\begingroup\$

MATL, 12 bytes

'&DtU'
&DtU

(the code has a trailing newline).

Try it online!

Explanation

'&DtU'    % Push this string
&D        % String representation (adds quote marks)
t         % Duplicate
U         % Evaluate (removes quote marks)
          % Implicitly display each string followed by a newline
\$\endgroup\$
7
  • \$\begingroup\$ I take it that it would not be shorter to do &D after t, avoiding U? \$\endgroup\$ Commented Oct 23, 2016 at 2:56
  • \$\begingroup\$ @ETHproductions Not sure if I understand your suggestion correctly. I think &D needs to be after t because the second part of the displayed output needs to be without quotes \$\endgroup\$
    – Luis Mendo
    Commented Oct 23, 2016 at 3:00
  • 1
    \$\begingroup\$ I mean that unevaling the string &D and then re-evaling U seems a little redundant. It's probably not shorter any other way, though, as you would likely need to use stack manipulation. \$\endgroup\$ Commented Oct 23, 2016 at 3:03
  • \$\begingroup\$ @ETHproductions Oh, now I see what you mean: this, right? (w is swap). As you say, it's not shorter unfortunately \$\endgroup\$
    – Luis Mendo
    Commented Oct 23, 2016 at 3:08
  • \$\begingroup\$ Yeah, that's what I meant, and that's what I figured \$\endgroup\$ Commented Oct 23, 2016 at 3:09
3
\$\begingroup\$

Noether, 30 bytes

"~a34BPaP34BPaP"~a34BPaP34BPaP

Try it online!

Basically, this works by pushing the string and storing it in the variable a, printing quotation marks (34B where B pushes the character with ASCII code 34) then printing a twice.

\$\endgroup\$
2
  • \$\begingroup\$ @OMᗺ That's only the Python/tio.run interpreter (which has a few bugs) . On the JS interpreter, there is no newline. \$\endgroup\$
    – Beta Decay
    Commented Aug 11, 2018 at 13:59
  • \$\begingroup\$ Alright, that's the one on TIO so I tried with that sorry! Nvm, in that case. \$\endgroup\$ Commented Aug 11, 2018 at 14:01
3
\$\begingroup\$

Flobnar, 94 bytes

	9	f;	/*+{ ;{?06	0/9^[]={o)	*4_;=	9);];36   
:
g<
0+,|!<|@17
10:_\^>|p*
+5`<>
:*<  ^ <47!!!

Try it online!

There's some debate over whether the get instruction counts as reading the source code, but in this case, I'm not using it to read the executed code, but a data array on the first line (except for specifically reusing just the 9 character).

This reads and prints the data section, then reads and prints each character incremented by one to represent the code section.

Explanation:

.
..
.....<|@..   Start at the @ going left
..........   We use the | to evaluate the code beyond it and come back here
.....
.............

:
g<
0+,|!<....   Print the character on the first line at position n plus 0
.0........   Then go down from the |
.....
.............

.9
.
..
...|!.....
..:_......   If n > 5*9 then go left at the _ and return !n
.5`<.
.*<..........

.
..
...|!<....   Else go right and increment n
1.._\^....   And loop again
+...>
:............

.
..           Once we've ended the loop we come back to the original | going down
.....<|.17   And we put a 1 at the (1,4) position and repeat the main code again
.1....>|p*   This time adding 1 to each character
.....
.....^ <47...
\$\endgroup\$
3
\$\begingroup\$

Runic Enchantments, 7 6 bytes

"'<~@>

Try it online!

Huh, a multiple pointer quine actually works pretty well.

Explanation:

"'<~@>
  <  >   Start pointers going left and right
"'<      Left pointer pushes " to their stack
    @    And terminate the IP, printing the stack (")
     >   Right pointer wraps around
"        Start string literal
 '<~@>   Push as string
"        End string literal
 '<~     Push the < character, but pop it
    @    Terminate the IP, printing the stack ('<~@>)
         End the program as there are no IPs left

Edit: I realised you can replicate this with only one pointer

'<~@|"

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Ha, didn't even see prior to posting this and you already beat me. Very clever use of the multiple IP feature. \$\endgroup\$ Commented Oct 1, 2018 at 18:22
1
6 7
8
9 10
16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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