A Quine on Every Line

30

2

Your goal is to create a program that prints itself indefinitely, with a new line after each one. Thus, if your program is a one-liner, it would be repeated on every line of the output.

Example

Program:

A

Output:

A
A
A
...

Rules

  • It must be a complete program, not a snippet or function.
  • The program should loop forever without stack-overflow or recursion limit errors.
  • Output is to stdout or closest alternative.
  • No program input is accepted.
  • Standard loopholes are disallowed, such as opening the program's file or accessing an external resource. Empty programs are disallowed as a standard loophole.
  • If your program code ends with a trailing newline, this does not count as the necessary newline between quines, and you must print another.
  • - Shortest code wins!

mbomb007

Posted 2015-09-11T14:15:46.397

Reputation: 21 944

The title looks like only one-line programs qualify? – Paŭlo Ebermann – 2015-09-16T18:40:45.427

@PaŭloEbermann Then read the problem description. It's a witty title because it rhymes. Here's an example of a valid multi-line program: http://codegolf.stackexchange.com/a/57985/34718 . You can't expect to know everything about a challenge by only reading the title.

– mbomb007 – 2015-09-16T18:51:38.157

@PaŭloEbermann It's just while(1)println(your_code); – Matthew Roh – 2017-04-21T23:41:50.147

Answers

19

Fission, 7 bytes

'!+!NR"

A rather simple modification of the shortest Fission quine I have found so far: I'm simply using the non-destructive ! instead of O and added an N for newline.

So all in all, here is how it works: control flow starts at the R with a right-going atom. " toggles string mode, which means everything until the next " is printed: in this case '!+!NR. That leaves the " and the newline to be printed. '! sets the atom's mass to 33, + increments it to 34 (the character code of ") and ! prints the quote. N prints a newline, and R is now a no-op in this case, so the loop starts over.

The following 7-byte solution also works:

"NR'!+!

Martin Ender

Posted 2015-09-11T14:15:46.397

Reputation: 184 808

9

><>, 16 bytes

'ard3*o50l2)?.~~

The traditional ><> quine uses too many os, so we use a loop for printing. Before each jump we push 5 and 0 (the coordinates of where to jump to), after which we either jump with . if there's still something to print, or pops the top two values with ~~.

(Reverted to the 16 version since I forgot about the stack overflow rule.)

Sp3000

Posted 2015-09-11T14:15:46.397

Reputation: 58 729

An alternative is "ar00go50l2)?.[, right? – mbomb007 – 2015-09-11T16:35:07.547

3@mbomb007 I guess, but I prefer d3* since g reads your own source code – Sp3000 – 2015-09-11T16:38:20.690

8

CJam, 13 bytes

{_o"_g
"o1}_g

The online interpreter doesn't print anything before the program terminates, so you'll have to test this in the Java interpreter.

Explanation

Finally a generalised CJam quine which doesn't end in _~.

{_o"_g
"o1}

This simply pushes a block. _g duplicates the block and executes it repeatedly while the top of the stack is truthy (discarding the condition).

Now inside the block, the other copy of the block is still on the stack. We duplicate and print it with _o and then we print _g followed by a newline (the required extra newline between quines) with "_g\n"o. Finally we push a 1 onto the stack for the loop to repeat, because unfortunately, blocks aren't truthy (or falsy) in CJam.

Martin Ender

Posted 2015-09-11T14:15:46.397

Reputation: 184 808

7

Python 2, 39

Not a very interesting task in Python as it's trivial to add the while loop to a normal quine.

c='while 2:print"c=%r;exec c"%c';exec c

feersum

Posted 2015-09-11T14:15:46.397

Reputation: 29 566

Doesn't this blow the stack? – Jesan Fafon – 2015-09-11T21:42:35.993

1@JesanFafon No, if you tried it you would see that it doesn't. There is no recursion. – feersum – 2015-09-11T21:53:26.317

6

Perl 5.10+, 40 37 bytes

$_=q{say"\$_=q{$_};eval"while 1};eval

or (also 37 bytes)

$_=q{{say"\$_=q{$_};eval";redo}};eval

Invoke with the -M5.010 or -E command line flag, e.g.

$ perl -E '$_=q{say"\$_=q{$_};eval"while 1};eval'
$_=q{say"\$_=q{$_};eval"while 1};eval
$_=q{say"\$_=q{$_};eval"while 1};eval
$_=q{say"\$_=q{$_};eval"while 1};eval
...

Thanks to Ilmari Karonen for shaving off 3 bytes from my original solution, which was:

eval while$_=q{say"eval while\$_=q{$_}"}

This, as well as the shorter 37-byte solutions above, are all simple variations of the following quine, which I first saw in one of Ilmari's other posts:

$_=q{say"\$_=q{$_};eval"};eval

Since all I added in my original solution was a while loop, he really deserves most of the credit. :-)

ThisSuitIsBlackNot

Posted 2015-09-11T14:15:46.397

Reputation: 1 050

Nice, and thanks for the credit. :-) The original quine is my own design, although someone else may well have independently discovered it earlier. BTW, $_=q{say"\$_=q{$_};eval"while 1};eval or $_=q{{say"\$_=q{$_};eval";redo}};eval would be a few bytes shorter. ($_=q{say"\$_=q{$_};eval";eval};eval would be even shorter yet, but I suspect it will eventually run out of stack.) – Ilmari Karonen – 2015-09-15T15:07:14.857

I was hoping you would show up! Thank you for the shorter solutions, I figured you would come up with something better; but I was so blown away by this when I saw it in the answer I linked to, I just had to try it myself. :) To be honest, I still haven't quite wrapped my head around quines, but the additional answers give me more to look at. I wasn't sure if you originated this because I saw you credited with the shortest non-cheating Perl quine ever(?!) on this page, but there's no attribution for this quine (using print instead of say).

– ThisSuitIsBlackNot – 2015-09-15T16:03:01.563

btw, the last one does indeed segfault after running for a while. Thanks again, @Ilmari! – ThisSuitIsBlackNot – 2015-09-15T16:03:48.070

Actually, I take that somewhat back -- I'm definitely not the first to invent that quine, and since I'd definitely seen it before (I posted the one I do claim some credit for in the same thread), it's quite likely that I subconsciously remembered it from that thread, or from somewhere else.

– Ilmari Karonen – 2015-09-15T16:16:23.427

Alright, I've left the origin unspecified. Regardless, thank you for the 3 bytes and thank you for the original inspiration. – ThisSuitIsBlackNot – 2015-09-15T16:39:45.563

5

Self-modifying Brainf*** (SMBF), 14 bytes

The trailing newline \n needs to be a literal, Unix, newline (ASCII code 10).

[<[<]>[.>]<.]\n

Explanation:

The code moves the pointer to the far left of its source code, then prints it all, including the newline (twice b/c of the rule). The loop continues.

mbomb007

Posted 2015-09-11T14:15:46.397

Reputation: 21 944

You mean, it has to be \r? – Ismael Miguel – 2015-09-11T17:27:53.140

@IsmaelMiguel No. \n is a new line, and is ASCII code 10, so a hex-dump of the source code would have the value 0A for that byte. http://code.cside.com/3rdpage/us/newLine.html

– mbomb007 – 2015-09-11T18:19:24.587

Then that is redundant because ALL newlines are assumed to be \n, unless specified otherwise. – Ismael Miguel – 2015-09-11T19:43:35.603

1@IsmaelMiguel Not true. When you type code, you actually hit the enter key for a newline. \n generally counts as 2 characters, and 2 bytes in your code. I typed it this way for readability, since the formatting doesn't work well for showing a blank line at the end of my source code. And so I had to specify that it was ONE byte, not two. – mbomb007 – 2015-09-11T19:46:17.197

2\n is always a single character, called a linefeed. A newline, however, is a platform-dependent sequence of bytes. – Dennis – 2015-09-11T22:32:02.463

3@Dennis Maybe so, but I don't care. I don't call / a "Solidus", and I don't know anybody who does. "Newline" starts with an n, and that's the escape character, so that's what I call it. It's closer to "line feed" than it is to "carriage return". – mbomb007 – 2015-09-12T01:02:26.863

3

PowerShell, 143 Bytes

$d='$d={0}{1}{0}{2}while(1){3}Write($d -f [char]39,$d,"`n",[char]123,[char]125){4}'
while(1){Write($d -f [char]39,$d,"`n",[char]123,[char]125)}

Based off the Rosetta Code PowerShell quine, I'm pretty confident this isn't the shortest possible. String replacement formatting in PowerShell is icky for this, because the same delimiters for where to put the replacements {} also delimit code blocks while{}, so we have to use [char] casting, which bloats the code a bunch.

AdmBorkBork

Posted 2015-09-11T14:15:46.397

Reputation: 41 581

3

Underload, 25 bytes

First time I've tried something like this and I'm not sure if follows all the rules as it is a couple of lines. The newline was a bit of a pain.

(::a~*S:a~*^
)::a~*S:a~*^

MickyT

Posted 2015-09-11T14:15:46.397

Reputation: 11 735

1

@mbomb007 The output per iteration is the two lines of the program. I tested it here

– MickyT – 2015-09-12T03:42:47.273

3

Befunge, 30 20 bytes

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

A variation of a popular befunge quine that prints out a newline and pops -1 on the stack if it finishes the line.
Unfortunately, Befunge gets verbose when doing things in one line. I tried to remove all of the launchpads (#) that i could, but some had to be left in to skip certain commands (like ,).

Changes:

30-20 -> changed the base quine to a custom one I made that uses string input. This way, the branching is a lot easier.

Old:

:0g,:93*`>0-10 #,+$#: #5 _1+>

I don't think this is optimal, but it's acceptable for now.

Kevin W.

Posted 2015-09-11T14:15:46.397

Reputation: 763

You can use a get instruction to grab a space, and add 2 to it for one byte shorter than 98+2/: <,+55,+2g1_ #!,#:<"

– MildlyMilquetoast – 2018-04-17T21:36:42.817

And push the quote and newline first and reverse the printer, -1g0:+5<>:#,_1"

– Jo King – 2018-04-17T22:22:48.923

2

Brachylog, 16 bytes

∋"∋~kgjẉ₁⊥"gjẉ₁⊥

You can't quite try it online, but I have verified that it works on my installation.

Brachylog, 18 bytes

∋"∋~kgjw₁⊥~n"gjw₁⊥

Try it online!

Adapted from the alternate version of this quine, with a backtracking hack I had initially devised for bogosort of all things. Spends two bytes on ~n because (the usual means for printing with a trailing newline) hits some odd issues with character encoding on TIO, and an actual newline inserted into the string literal gets printed through ~k as \n.

               w      Print
 "∋~kgjw₁⊥~n"         "∋~kgjw₁⊥~n"
∋                     which is an element of
                      the (unused and unconstrained) input
                ₁     formatted
          ~n          (so that the ~n is replaced with a newline)
             gj       with itself
   ~k                 (so that the ~k is replaced with the string in quotes),
                 ⊥    then try again.

Since w₁ takes input as a list [string to be formatted, formatting arguments], wrapping a string in a list with g then concatenating it to itself with j allows it to be formatted with itself. And since no input is given to the program, the input variable which is implicitly present at the beginning of the program can take on any value, so it can be constrained to one of the infinitely many lists which contain "∋~kgjw₁⊥~n" as an element, creating a single choice point to be backtracked to when after printing its source the program hits .

Unrelated String

Posted 2015-09-11T14:15:46.397

Reputation: 5 300

2

Python 3.6, 48 43 bytes.

-5 bytes thanks to @Jo King

x='while 1:print("x=%r;exec(x)"%x)';exec(x)

Divy

Posted 2015-09-11T14:15:46.397

Reputation: 51

You can use %r to save on escaping those 's. 43 bytes

– Jo King – 2019-04-15T09:50:12.027

which gives 41 for python 3.8 :P

– ASCII-only – 2019-04-16T12:48:52.643

2

Gol><>, 10 bytes

"r2sslRoao

Try it online!

This can probably be golfed down more

KrystosTheOverlord

Posted 2015-09-11T14:15:46.397

Reputation: 681

2

><>, 31 29 bytes

A simple modification of the traditional ><> quine.

"ar00gc0.0+efooooooooooooooo|

To run, paste it here, click 'Submit', then 'Start' (running without animation doesn't work). Feel free to increase the execution speed.

mbomb007

Posted 2015-09-11T14:15:46.397

Reputation: 21 944

"ar00gc0.0+feooooooooooooooo| is two bytes shorter. – cole – 2015-09-12T03:56:01.570

@Cole Thanks. I had thought about the mirror, but forgot to change it after getting side-tracked on a different challenge. – mbomb007 – 2015-09-12T19:38:11.170

20 bytes – Jo King – 2019-04-15T06:58:33.543

@JoKing There's already a shorter ><> solution.

– mbomb007 – 2019-04-15T14:30:23.183

2

K, 30 bytes

{f:$_f;{x}{`0:f,"[]\n";1}/1}[]

kirbyfan64sos

Posted 2015-09-11T14:15:46.397

Reputation: 8 730

2

R, 34 bytes

repeat{write(commandArgs()[5],'')}

to invoke from the command line as follows:

Rscript -e "repeat{write(commandArgs()[5],'')}"

flodel

Posted 2015-09-11T14:15:46.397

Reputation: 2 345

2

Python 2, 48 bytes

c='c=\'%s\';while 1:print c%c';while 1:print c%c

TheInitializer

Posted 2015-09-11T14:15:46.397

Reputation: 829

2

GolfScript, 16 bytes

{.".do
":n\p}.do

This ended up looking a lot like Martin Büttner's CJam entry. An interesting feature is that, as it turns out, the shortest way to append ".do" to the block when it's printed is to assign it to the line terminator n. (Of course, we also need to include an actual newline in the string, to replace the one n normally contains.) The same string (being truthy in GolfScript) is also left on the stack for the do loop to pop off, ensuring that the loop runs forever.

Ilmari Karonen

Posted 2015-09-11T14:15:46.397

Reputation: 19 513

1

PowerShell, 132 107 Bytes

$a='$a={0}{1}{0};for(){2}$a-f[char]39,$a,[char]123,[char]125{3}';for(){$a-f[char]39,$a,[char]123,[char]125}

Based off of the Rosetta Quine (Same as @AdmBorkBork) although doesn't use formatting for string replacement... maybe switching to a for loop and using formatting would be best?

I'm sure if AdmBorkBork came back they would beat this by a lot :P

EDIT Figured out the for loop and replacements, all thanks to my predecessor :)

Old attempt:

$a='$a=;for(){$a.substring(0,3)+[char]39+$a+[char]39+$a.substring(3)}';for(){$a.substring(0,3)+[char]39+$a+[char]39+$a.substring(3)}

Sinusoid

Posted 2015-09-11T14:15:46.397

Reputation: 451

1

Ruby, 41 39 bytes

A modification of one of the Ruby quines found here.

_="_=%p;loop{puts _%%_}";loop{puts _%_}

Value Ink

Posted 2015-09-11T14:15:46.397

Reputation: 10 608

1

Java 10, 194 bytes

interface M{static void main(String[]a){for(var s="interface M{static void main(String[]a){for(var s=%c%s%1$c;;)System.out.println(s.format(s,34,s));}}";;)System.out.println(s.format(s,34,s));}}

Explanation:

Try it online (times out after 60 sec).

interface M{                    // Class
  static void main(String[]a){  //  Mandatory main-method
    for(var s="interface M{static void main(String[]a){for(var s=%c%s%1$c;;)System.out.println(s.format(s,34,s));}}";
                                //   Unformatted source code
        ;)                      //   Loop indefinitely
       System.out.println(      //    Print with trailing new-line:
         s.format(s,34,s));}}   //     The formatted source code

-part:

  • The String s contains the unformatted source code.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together

Challenge part:

  • for(;;) is used to loop indefinitely.
  • System.out.println(...) is used to print with trailing new-line

Kevin Cruijssen

Posted 2015-09-11T14:15:46.397

Reputation: 67 575

1

Funky, 29 bytes

f=@print`f=[f]for)f()`for)f()

Try it online!

ATaco

Posted 2015-09-11T14:15:46.397

Reputation: 7 898

1

RProgN 2, 7 bytes

«]³]]p:

Try it online!

ATaco

Posted 2015-09-11T14:15:46.397

Reputation: 7 898

1

Jelly, 14 bytes

“;@ṾṄṛ¢”;@ṾṄṛ¢

Try it online!

dylnan

Posted 2015-09-11T14:15:46.397

Reputation: 4 993

1

Pyth, 11 bytes

#jN B"#jN B

Try it here!

RK.

Posted 2015-09-11T14:15:46.397

Reputation: 497

It works well enough. Just pause execution shortly after running. – mbomb007 – 2018-04-17T02:05:04.270

Huh, nice! I'll edit that link in. – RK. – 2018-04-17T13:59:45.710

1

Keg, 16 bytes

`{④⑩9⑨,}`{④⑩9⑨,}

Try it online!

Ties with most other golfing languages, so I'm happy with that. Pushes the string {④⑩9⑨,}, which is then infinitely printed rawly, then nicely followed by a newline infinitely.

Lyxal

Posted 2015-09-11T14:15:46.397

Reputation: 5 253

1

BASH, 76 bytes

Just couldn't resist, especially with PowerShell here :)

while true;do
a ()
{
    echo while true\;do
    declare -f a
    echo a\;done
}
a;done

The whitespace is important for an EXACT copy.

RK.

Posted 2015-09-11T14:15:46.397

Reputation: 497

1

Batch, 10 (+ 4 for filename length)

Not sure if this qualifies for two reasons:

  • Technically, there may or may not be textual side effects from the Windows command shell, as that depends on how it is configured.
  • This program invokes itself by name, and I am not sure whether that's prohibited by the rules (specifically the "no opening of program file" rule). It is not opening itself for the purposes of reading and printing out the text; it is simply re-running itself. Further, the file system structure is an integral part of old-school batch scripts (oftentimes even being used to store program state, etc). As such, I am not sure whether this violates the 5th rule or not.

The code (for a program named q.bat):

echo on&&q

Ruslan

Posted 2015-09-11T14:15:46.397

Reputation: 280

Hmm, I'm not sure. Does it print it forever to the same output stream? – mbomb007 – 2015-09-12T01:06:12.197

@mbomb007 Yup... – Ruslan – 2015-09-12T01:15:25.123

2I get I'll allow it since it's not the shortest, and it's creative and unique. And it wasn't reading the source code, but rather executing itself. – mbomb007 – 2015-09-12T03:37:11.560

1Since the file name is not arbitrary, at the very least you should add the file name to the byte count. – Martin Ender – 2015-09-12T05:40:14.547

2I believe you could use echo on&&%0 although I'm afraid to try it. – DankMemes – 2015-09-12T19:12:30.100

1Update: echo on&&%0 does not work. %0 is displayed as expanded in the output, and windows (7, 32 bit, running in a VM) terminates the entire thing pretty quickly. – DankMemes – 2015-09-12T19:50:01.977

@DankMemes Haha yes, that was the first thing I tried :) – Ruslan – 2015-09-13T07:52:27.863

1I think you can omit the .bat part – SuperJedi224 – 2015-09-15T19:01:29.150

@SuperJedi224 Right! Thanks for that – Ruslan – 2015-09-16T07:15:11.027

1

Javascript (ES6), 64

for(;;(_=>console.log(`for(;;(_=>${arguments.callee})());`))());

Alternatively (also 64)

a="for(;;)console.log(`a=${JSON.stringify(a)};eval(a)`)";eval(a)

DankMemes

Posted 2015-09-11T14:15:46.397

Reputation: 2 769

Wouldn't the alternative cause a stack overflow? – Patrick Roberts – 2015-09-13T13:23:19.200

No. There is no recursion so nothing should be adding up on the stack. – DankMemes – 2015-09-13T15:27:55.440

Okay, well I just tested both of them, you are correct about the alternative solution, but your first solution fails and I figured out why. According the MDN documentation on arrow functions, "Arrow functions do not expose an arguments object to their code" so your arguments.callee does not exist and throws an error.

– Patrick Roberts – 2015-09-13T17:47:30.217

Interesting. For me in Firefox Developer Edition 42.0a2 (2015-09-13), both work. I guess once the standard is fully adopted the first one will stop working. – DankMemes – 2015-09-13T17:52:07.827

I tested them on latest chrome since I was trying to golf your first solution with ! instead of enclosing parentheses and putting the function execution after the for loop to avoid the need for the for loop semicolon body which would save (I think) 4 bytes – Patrick Roberts – 2015-09-13T17:54:32.243

1

Microscript, 22 bytes

"f1{CqxCanx"f1{CqxCanx

Based on the quine from the Esolangs article: "fCqxah"fCqxah. Exploits the fact that the language autoappends closing braces as needed, without which this would be two bytes longer.

SuperJedi224

Posted 2015-09-11T14:15:46.397

Reputation: 11 342

1

Ceylon, 210 208 bytes

Of course this won't win anything...

shared void u(){value q="\"\"\"";value t="""shared void u(){value q="\"\"\"";value t=""";value b="""while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}""";while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}

Original:

shared void u(){value q="\"\"\"";value t="""shared void u(){value q="\"\"\"";value t=""";value b="""while(true){print(t+q+t+q+";value b="+q+b+q+";"+b);}}""";while(true){print(t+q+t+q+";value b="+q+b+q+";"+b);}}

I modified my Quine from two days ago by adding the while(true){...} loop, so I come from the 185 bytes of the plain Quine to 210 (I don't need the trailing new line character anymore). But then I found that a while(1<2){...} loop is even two bytes shorter:

shared void u(){value q="\"\"\"";value t="""shared void u(){value q="\"\"\"";value t=""";value b="""while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}""";while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}

(Ceylon has no for(;;) loop like Java, and the braces are also needed for this loop.)

Paŭlo Ebermann

Posted 2015-09-11T14:15:46.397

Reputation: 1 010