Self-growing code codee codeee codeeee

42

3

Write a program (or function) (let's call it P1), that when run, outputs another program P2 of the same language and exactly 1 byte longer than P1.

Program P2 when run, should output a 3rd program P3 that's 1 byte longer than P2. P3 must output a program P4 that's one byte longer than P3, etc. The same for P5, P6, ..., P∞.

The program chain should go indefinitely, or to a place where the interpreter can't handle anymore (but must remain as a theoretically valid program in the language)

Rules

  • Standard loopholes forbidden
  • All programs in the chain should be in one language
  • No input is given. Output goes to stdout or function return value
  • The program must end after a period of time. A program that stops generating output after a certain time spot but never terminates does not qualify

The shortest program P1 in bytes in each language wins!

iBug

Posted 2018-03-17T04:48:42.210

Reputation: 2 477

Since the quine tag was added after the three current answers were posted, were you intending to forbid the quine loopholes in addition to the other ones? – Οurous – 2018-03-17T05:39:37.763

2@Οurous What??? I did not add that tag myself... – iBug – 2018-03-17T06:45:15.957

6@iBug Regardless, are submissions allowed to read their own source code? – Martin Ender – 2018-03-17T06:45:59.947

@MartinEnder What's the site's stance or general case? I did forbid it at the beginning, but I removed it during the grace period and I'm now unsure. I tend to forbit it now. – iBug – 2018-03-17T08:12:26.570

3@iBug The "quine" tag forbids it by default, and it usually makes answers more interesting to do so. It's up to you though. – Martin Ender – 2018-03-17T08:15:43.677

More restrictive challenge but highly related. – Giuseppe – 2018-03-17T10:50:51.353

1"puts <<2*2,2\nputs <<2*2,2\n\n2" grows by 2 at each iteration in Ruby. I couldn't find anything better. :-/. Interesting challenge! – Eric Duminil – 2018-03-17T20:17:17.370

less restricted but highly related – Destructible Lemon – 2018-03-18T22:43:36.133

1

Possible duplicate of Program that creates larger versions of itself (quine-variant)

– FireCubez – 2018-12-05T15:52:53.530

Answers

29

JavaScript (ES6), 14 12 bytes

-2 bytes thanks to @Shaggy

f=_=>"f=_"+f

Testing snippet

s=`f=_=>"f=_"+f`;

for(i=10;i--;s=eval(s)())
  o.innerText+=s+"\n";
<pre id=o></pre>

Herman L

Posted 2018-03-17T04:48:42.210

Reputation: 3 611

Took me a second to spot that. Sneaky! – Shaggy – 2018-03-17T08:40:02.687

4Can someone please explain, I cannot wrap my head around it, how does it increase? – htmlcoderexe – 2018-03-17T11:29:25.920

2@htmlcoderexe "f=_" prepends an extra _ before the parameter name, which causes it to increase in length every iteration. – Herman L – 2018-03-17T11:34:26.467

9

Haskell, 74 66 bytes

EDIT:

  • -2 bytes by H.PWiz by using <>, then -6 by moving the (10*)<$>.

This now uses the newly free <> operator (Semigroup multiplication, requires GHC 8.4 to work without an import.)

main=putStr$fst<>show$(10*)<$>("main=putStr$fst<>show$(10*)<$>",1)

Try it online! (Cheats with an import since TIO doesn't have GHC 8.4 yet.)

How it works

  • main=putStr$ is boilerplate to output the following string value.
  • fst<>show is a function that takes a tuple, and returns a string consisting of the first element of the tuple concatenated with the tuple's string representation. I.e.

    (fst<>show)(s,t) = fst(s,t)<>show(s,t) = s++show(s,t)
    
  • (10*)<$> multiplies the last element of the following tuple by 10, adding a digit 0 to its string representation.

Ørjan Johansen

Posted 2018-03-17T04:48:42.210

Reputation: 6 914

1

You can save at least 2 bytes with (<>)

– H.PWiz – 2018-04-02T19:31:10.067

@H.PWiz Thanks, got some more by moving (10*)<$>. – Ørjan Johansen – 2018-04-03T00:50:20.467

9

7, 4 bytes of ASCII

1603

Try it online!

I know that 7 isn't normally encoded in ASCII, but this time it's a more convenient encoding so that we're adding 1 byte with each run, not 3 bits.

I'm also not sure whether this counts as cheating or not. (It's usual to be unclear whether or not a 7 quine is cheating, as it straddles the borderline in a number of ways.) You can make a decent argument that the 0 encodes the 6, but in general it's unclear where the resulting characters "come from" in 7 because it has so many, fairly bizarre, implicit behaviours.

This program prints itself with 1 appended, and will do so even if you append a number of 1s to it. Here's a commented debug trace of 160311:

|| 160311      Initial data ||; initial program 160311
||7 60311      1 command = append 7 to data
|1 0311        6 command = escape from the last | onwards (7 escapes to 1)
|16e77         0311 commands = append 6e77 to data
|16e77 16e77   Implicit (program is empty): copy data past last | to program
|16e777 6e77   1 command = append 7 to data
71603111 e77   6 command = escape from the last | onwards
71603111 e77   e7 command = output in same encoding as the source

(There are no | left in the program, so e will immediately quit the program as a side effect, meaning that the final 7s never run).

The basic confusion about where all the characters are coming from is that most commands in 7 just produce data when run, and then 6 attempts to reconstruct a sequence of commands that would produce the given fragment of data; this often ends up close to, but not identical to, the original. (For quining purposes, you normally write a 7 program in such a way that the result will be almost the same, normally differing in leading or trailing 7s.) So for example, the 1 in the data becomes 716, which is the easiest way to append 1 to the current data string. We originally produced it with 16, a different (but similar) character sequence, destructively removing one of the | markers the data started with. (I guess perhaps the best argument that this isn't a literal-only quine is that the output is different from the input!)

ais523

Posted 2018-03-17T04:48:42.210

Reputation: 11

8

C (gcc), 134 132 bytes

Slight reworking of the canonical C quine. Dreadfully long.

x;*s="x;*s=%c%s%c;main(i){for(i=__LINE__;i--;puts(&x));printf(s,34,s,34);}";main(i){for(i=__LINE__;i--;puts(&x));printf(s,34,s,34);}

Try it online!

gastropner

Posted 2018-03-17T04:48:42.210

Reputation: 3 264

7

Pari/GP, 35 bytes

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

Try it online!

alephalpha

Posted 2018-03-17T04:48:42.210

Reputation: 23 988

7

Fission 2, 7 bytes

'!+OR!"

Try it online!

The standard Fission quine, but with an extra ! that prints an additional 0x01 at the beginning of the code on each iteration.

Martin Ender

Posted 2018-03-17T04:48:42.210

Reputation: 184 808

4

Python 2, 38 bytes

s="print's=%r;exec s'%(s+'#')";exec s

Try it online!

totallyhuman

Posted 2018-03-17T04:48:42.210

Reputation: 15 378

4

brainfuck, 420 bytes

->+++>>+++>+>++>+++>+>+>+++>>>>>>>>>>>>>>>>>>>>+>+>++>+++>++>>+++>+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+>>+++>>+++>>>>>+++>+>>>>>>>>>++>+++>+++>+>>+++>+++>+>++>>+++>+>+++>+>++>+++>>>+>+>++>+++>+>+>>+++>>>>>>>+>+>>>+>+>++>+++>+++>+>>+++>>>+++>+>++>+++>++>>+>+>++>+++>+>+>>+++>>>>>+++>+>>>>>++>+++>+++>+>>+++>>>+++>+>+++>+>>+++>>+++>>++[[>>+[>]++>++[<]<-]>+[>]<+<+++[<]<+]>>->[>]++++>++[[<++++++++++++++++>-]<+++++++++.<]>.

Try it online!

This is a modification on the "standard" BrainFuck quine, with a . at the end that tacks on an extra . each iteration.

The quine itself encodes Brainfuck characters as a stack of hex digits: specifically, the hex digits of c-0x2b, which conveniently are the following:

+: 0x00
-: 0x02
[: 0x30
]: 0x32
<: 0x11
>: 0x13
,: 0x01
.: 0x03

The encoding covers two snippets of code: >++[[>>+[>]++>++[<]<-]>+[>]<+<+++[<]<+]>>->[>]++++>++ pushes the encoding of the encoding itself, and [[<++++++++++++++++>-]<+++++++++.<] walks the stack and prints everything.

nneonneo

Posted 2018-03-17T04:48:42.210

Reputation: 11 445

3

Dirty, 9 bytes

'"n[!]a!␛

Try it online!

'   start and end a string literal
"   push a literal '
n   remove newlines
[!] print the string
a   push the alphabet
!   print the first character
␛   end the program

If source-code reading is allowed:

Dirty, 8 bytes

Q[!]W33!

Try it online!

Explained:

Q   push the source code
[!] print each character
W   clear the now-empty stack
33! print an exclaimation mark

Might be valid:

Dirty, 4 bytes

Q[‼]

Try it online!

It prints the source code with a trailing newline.
(And a bunch of spaces, due to a bug. It works the same without them though.)

Note that it only works in the native character set, and not when you use the UTF8 front-end - so to try it on TIO you need to replace the character it outputs between the []s with , which is the UTF8 equivalent for what it's printing.

Οurous

Posted 2018-03-17T04:48:42.210

Reputation: 7 916

1The 4-byte version is definitely invalid. – Erik the Outgolfer – 2018-03-17T11:30:42.310

3

Java 8, 162 146 bytes

v->{String s="v->{String s=%c%s%1$c+1;return s.format(s,34,s).replaceAll(%1$c1+$%1$c,%1$c%1$c);}"+1;return s.format(s,34,s).replaceAll("1+$","");}

Try it online.
Try the first output program; Try the second output program; Try the third output program.

Explanation:

v->{                       // Method with empty unused parameter and String return-type
  String s="v->{String s=%c%s%1$c+1;return s.format(s,34,s).replaceAll(%1$c1+$%1$c,%1$c%1$c);}"
                           //  The unformatted source code
           +1;             //  Plus a random digit (1 in this case)
  return s.format(s,34,s)  //  Create the quine
          .replaceAll("1+$","");}
                           //  Then remove any trailing 1s

-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:

  • +1 adds a 1 to both the unformatted and formatted program.
  • .replaceAll("1+$","");}: Because we only want to increase the program byte-count by one instead of two, we remove any trailing 1s before returning.

Kevin Cruijssen

Posted 2018-03-17T04:48:42.210

Reputation: 67 575

2

><>, 9 bytes

#o<:}-1:"

Try it online!

Variation on a classic ><> quine, which simply adds another dupe command to copy the # in the front.

Jo King

Posted 2018-03-17T04:48:42.210

Reputation: 38 234

2

Perl 5, 32 bytes

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

Try it online!

Dom Hastings

Posted 2018-03-17T04:48:42.210

Reputation: 16 415

2

GolfScript, 9 bytes

{'.~1'}.~

Try it online!

CJam, 9 bytes

{"_~1"}_~

Try it online!

I'm posting both of these solutions in the same answer, since they're just trivial variations of each other and work in exactly the same way. They're both based on the common GolfScript quine {'.~'}.~ (or {"_~"}_~ in CJam), which is described in more detail e.g. in this previous answer of mine.

The only difference is that this variant appends a 1 byte to the end of its output. As it happens, any string of 1s (or any other integer literal without leading zeros) is itself a trivial quine in both GolfScript and CJam, so any ones already present at the end of the code above will simply be copied verbatim to the output. Since GolfScript (and CJam) use arbitrary-length integers, this will work for arbitrarily long programs, at least as long as the computer running the code has enough memory to store it.

Ilmari Karonen

Posted 2018-03-17T04:48:42.210

Reputation: 19 513

2

Attache, 76 72 61 bytes

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]]",Repr[x+sp]]

Try it online!

Standard quine which adds a space to the end of x after each iteration.

First few iterations:

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]]",Repr[x+sp]]

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]] ",Repr[x+sp]]

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]]  ",Repr[x+sp]]

etc.

Attache, 72 bytes

y:=1Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

Try it online!

This is simply a variation of the standard format quine, with a variable y that is set to 10*y after each iteration

The first few iterations:

y:=1Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

y:=10Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

y:=100Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

etc.

Conor O'Brien

Posted 2018-03-17T04:48:42.210

Reputation: 36 228

2

Jstx, 4 bytes

£↕♣:

Try it online!

Quantum64

Posted 2018-03-17T04:48:42.210

Reputation: 371

2

Runic Enchantments, 6 bytes

"'<S@>

Try it online!

This one was weird. All I had to do was remove a ~ from the original quine found by Jo King.

Every additional run appends another < to the end, e.g.:

"'<S@><<<<<<<<<

All of which do nothing.

Direct copy of this answer on a related challenge. It just so happened to already grow by 1 byte every iteration (strong argument for this challenge being a duplicate of that one or vice versa).

Draco18s no longer trusts SE

Posted 2018-03-17T04:48:42.210

Reputation: 3 053

1

Swift 4, 89 bytes

let(a,b)=(";print(\"let(a,b)=\\((a,b+\"-\"))\"+a)", "");print("let(a,b)=\((a,b+"-"))"+a)

Includes a trailing newline

Try it online!

Herman L

Posted 2018-03-17T04:48:42.210

Reputation: 3 611

1

Haskell, 88 bytes

main=putStr$snd(span(<'m')s)++show s;s='#':"main=putStr$snd(span(<'m')s)++show s;s='#':"

Try it online! Grows by prepending # to the data string.

Laikoni

Posted 2018-03-17T04:48:42.210

Reputation: 23 676

You can save a bit by showing more than just a string and using pattern matching. Try it online!

– Ørjan Johansen – 2018-03-17T15:59:50.633

@ØrjanJohansen Nice! Except for the underlying standard Haskell quine this is a completely different approach, so feel free to post it yourself. – Laikoni – 2018-03-17T16:05:34.263

OK, if you think so. – Ørjan Johansen – 2018-03-17T16:20:19.367

1

Wonder, 33 bytes

f\ @(-> ol) ["f\ ";f;";f1";#0];f1

An interesting variant on the normal quine that appends a 1 after each iteration.

Progression:

f\ @(-> ol) ["f\ ";f;";f1";#0];f1
f\ @(-> ol) ["f\ ";f;";f1";#0];f11
f\ @(-> ol) ["f\ ";f;";f1";#0];f111
...

Explanation

f\ @                               #. Sets f to a function that does the following:
    (-> ol) [                      #.   Output each:
             "f\ ";                #.     String for declaration of f
                   f;              #.     Formatted representation of f's function
                     ";f1";        #.     String for call of f
                           #0      #.     Argument passed into f
                             ];f1  #. Call f with 1 as the argument

One of the interesting parts of this quine is that Wonder can work with numbers of arbitrary precision, so the progression won't break after a certain amount of ones.

Mama Fun Roll

Posted 2018-03-17T04:48:42.210

Reputation: 7 234

1

Stax, 20 18 bytes

"34s+cTZL"34s+cTZL

Run and debug it

Generates an extra space before the 2nd quotation mark every iteration.

Explanation

Uses the program "34s+cTZL "34s+cTZL to explain.

"34s+cTZL "34s+cTZL
"34s+cTZL "            String literal
           34s+        Prepend a double quote, Now the string is `"34s+cTZL `
               cT      Copy and trim trailing spaces
                 Z     Put a 0 under the top of stack
                       Stack now (from top to bottom): `["34s+cTZL,0,"34s+cTZL ]`
                  L    Collect all elements on stack, from bottom to top
                       Implicit output, 0 is converted to space.

Weijun Zhou

Posted 2018-03-17T04:48:42.210

Reputation: 3 396

1

Gol><>, 7 bytes

":r2ssH

Try it online! Try it online!! Try it online!!!

This is a simple variation of the regular Gol><> quine but adds a copy of H each time it's run. Since the H command halts the program, additional H at the end does not change the behavior.

Bubbler

Posted 2018-03-17T04:48:42.210

Reputation: 16 616

1

C# (Visual C# Interactive Compiler), 89 bytes

var s="var s={0}{1}{0}+8;Write(s.Trim('8'),(char)34,s);"+8;Write(s.Trim('8'),(char)34,s);

Inspired by Kevin Cruijssen's answer.

Try it online!

Embodiment of Ignorance

Posted 2018-03-17T04:48:42.210

Reputation: 7 014

0

Jelly, 11 bytes

“⁾;⁶;ØV” ṘV

Try it online!

user202729

Posted 2018-03-17T04:48:42.210

Reputation: 14 620

0

Windows Batch, 38 36 bytes

echo|set/p"=q">q&copy/b/y %0+q %0
::

This code creates a file called "q", containing the letter 'q', and then appends it to the original file. Note that "::" is an alias for "rem" that does not require an additional space.

Saved 2 bytes thanks to user3493001.

peter ferrie

Posted 2018-03-17T04:48:42.210

Reputation: 804

0

ColdFusion, 277 bytes

<cfset u=Chr(34)><cfset q="<cfset u=Chr(34)><cfset q=%s%s%s><cfoutput>%screateObject(%sjava%s,%sjava.lang.String%s).format(q,[u,q,u,Chr(35),u,u,u,u,Chr(35)])%s</cfoutput>
"><cfoutput>#createObject("java","java.lang.String").format(q,[u,q,u,Chr(35),u,u,u,u,Chr(35)])#</cfoutput>

This is a trivial modification of my ColdFusion quine which adds a newline each extra time it's called.

Tested locally on lucee-express-5.2.6.60

Dom Hastings

Posted 2018-03-17T04:48:42.210

Reputation: 16 415

0

T-SQL, 175 bytes

DECLARE @ VARCHAR(MAX)='DECLARE @ VARCHAR(MAX)=*SET @=TRIM(REPLACE(@,0x2a,CHAR(39)+@+CHAR(32)+CHAR(39)))PRINT @'SET @=TRIM(REPLACE(@,0x2a,CHAR(39)+@+CHAR(32)+CHAR(39)))PRINT @

First I wrote an SQL quine, then I modified it to add an extra space (somewhat inspired by this answer).

Razvan Socol

Posted 2018-03-17T04:48:42.210

Reputation: 341

0

Befunge-98 (PyFunge), 22 19 bytes

r:#,_ #;'D2/'<,,@;"

Try it online!

Adds a < before the last " every generation.

IQuick 143

Posted 2018-03-17T04:48:42.210

Reputation: 1 229