Generate Programs in Increasing size

21

In this challenge, you will write a program which outputs a program which is the double the length of the original program. The outputted program should output a new program a program double its length.

Examples

If my program is a:

< a
> aa

< aa
> aaaa

< aaaa
> aaaaaaaa

Rules

  • No quine built-ins
  • The original program must be at least one byte
  • The sequence should theoretically work infinitely
  • Your program is not allowed to read from anything (file, stdio)

Your score is the size of the original program.

Downgoat

Posted 2016-01-15T23:31:32.083

Reputation: 27 116

How about accessing the file by reading itself/accessing the place where the code is contained? – Conor O'Brien – 2016-01-15T23:36:57.927

@CᴏɴᴏʀO'Bʀɪᴇɴ that's not allowed as that would violate rules I believe. – Downgoat – 2016-01-15T23:37:37.203

It's not a quine built-in. e.g. document.getElementById("code") or something. – Conor O'Brien – 2016-01-15T23:38:24.073

@CᴏɴᴏʀO'Bʀɪᴇɴ oh whoops, I left out a few words :| I meant to say "that would violate our quine rules, I believe" – Downgoat – 2016-01-16T00:24:35.287

Oh, I see. I didn't think this was a quine challenge. – Conor O'Brien – 2016-01-16T00:27:18.130

2@Doᴡɴɢᴏᴀᴛ the quine rules are recommended by the tag wiki but don't apply automatically – Martin Ender – 2016-01-16T00:27:45.330

I accidentally wrote a program that did this while working on the growing quine challenge – quintopia – 2016-01-16T00:31:08.503

2Related. Related. Related. – Martin Ender – 2016-01-16T11:39:33.597

Answers

12

CJam, 10 bytes

{"_~"+_}_~

Test it here.

Explanation

{"_~" e# Generalised quine framework, leaves the block and the string "_~"
      e# on the stack. 
+     e# Prepend the block to the string.
_     e# Duplicate the resulting array.
}_~

Martin Ender

Posted 2016-01-15T23:31:32.083

Reputation: 184 808

GolfScript isn't dead! Ilmari has an answer to a similar question that does this in exactly 10 bytes.

– Justin – 2016-01-17T09:19:19.053

8

JavaScript, 62 61 37 bytes


Thanks to @Doᴡɴɢᴏᴀᴛ for the help!


Original [37 bytes]:

f=_=>'f='+'_'.repeat((10+f).length)+f

Child [74 bytes]:

f=______________________________________=>'f='+'_'.repeat((10+f).length)+f

Grandchild [148 bytes]:

f=________________________________________________________________________________________________________________=>'f='+'_'.repeat((10+f).length)+f


Alternate (with printing to console, and as a full program):

Original [61 bytes]:

f=_=>console.log(`f=${'_'.repeat((0+f).length+5)+f};f()`);f()

Child [122 bytes]:

f=______________________________________________________________=>console.log(`f=${'_'.repeat((0+f).length+5)+f};f()`);f()

Grandchild [244 bytes]:

f=________________________________________________________________________________________________________________________________________________________________________________________=>console.log(`f=${'_'.repeat((0+f).length+5)+f};f()`);f()



How it works!

1.   f=_=>   Define function f as console.log(...)

2.   ;f()   Run function f.

3.   (in function f)  

  • console.log(...)   Print the following:

    • f=   literal text "f="
    • ${'_'.repeat((0+f).length+5)   "_" repeated for the length of f, altered to account for characters not included in the stringification of f
    • +f}   The stringification of function f
    • ;f()   literal text ";f()"

Notes

  • console.log is necessary instead of alert because alert doesn't seem to play well with really long strings (at least on my machine/browser configuration)
  • The _'s are inserted into the name of the (unused) parameter of function f, to ensure that they are included in the stringification of f.
  • Main improvement (aside from getting rid of the console.log) of the first solution over the second: adding 10 to the function instead of 0 to cast it to string makes it one byte longer, eliminating the need to add 1 to the length afterwards, saving a byte.

jrich

Posted 2016-01-15T23:31:32.083

Reputation: 3 898

0+f should also work to cast the function to a string – Downgoat – 2016-01-16T01:41:48.580

48 bytes: (f=_=>\(f=${'_'.repeat((0+f).length+5)+f})()`)()` – Downgoat – 2016-01-16T02:29:15.060

@Doᴡɴɢᴏᴀᴛ Forgot that returning the result is usually acceptable. Will update. – jrich – 2016-01-16T02:51:45.077

@Doᴡɴɢᴏᴀᴛ Actually, since writing answers as functions is generally accepted, does the solution even have to call the function? – jrich – 2016-01-16T02:53:56.313

sure, you could do – Downgoat – 2016-01-16T02:55:18.120

Actually, I'm pretty sure the solution has to call the function because the quine function can't output anything by itself (without the call). – Mama Fun Roll – 2016-01-16T18:14:32.467

6

Minkolang 0.15, 19 14 bytes

"66*2-rIDdr$O.

Original, child, grandchild.

Explanation

"66*2-      $O.    Standard quine formulation

      r            Reverse stack
       I           Push length of stack
        D          Pop n and duplicate top of stack n times
         d         Duplicate top of stack
          r        Reverse stack

What the bit in between rs does is duplicate the ending period enough times to fulfill the doubling criterion. . is the "stop program" character, so the many periods at the end do nothing except be there.

El'endia Starman

Posted 2016-01-15T23:31:32.083

Reputation: 14 504

3

CJam, 12 bytes

{"_~"1$1$}_~

When run, this will print

{"_~"1$1$}_~{"_~"1$1$}_~

which, in turn, will print

{"_~"1$1$}_~{"_~"1$1$}_~{"_~"1$1$}_~{"_~"1$1$}_~

and so on.

Try it online!

Dennis

Posted 2016-01-15T23:31:32.083

Reputation: 196 637

2

Python 3, 51 bytes

x=r"print('x=r\"'+x+'\"'+';exec(x*2)');";exec(x*2)

This includes a trailing newline.

Which outputs:

x=r"print('x=r\"'+x+'\"'+';exec(x*2)');";exec(x*2)
x=r"print('x=r\"'+x+'\"'+';exec(x*2)');";exec(x*2)

Zweedeend

Posted 2016-01-15T23:31:32.083

Reputation: 121

2

GolfScript, 11 bytes

{: ".~"]}.~

Try it online!

How the source code works

{: ".~"]}.~

{       }    Define and push a code block.
         .~  Push a copy and execute it.
 :           Save the code block in the space character.
             Every subsequent space will now execute the code block.
   ".~"      Push that string.
       ]     Wrap the entire stack in an array.

If the above source code is executed once, the stack will end up as

["" {: ".~"]} ".~"]

where the empty string at the beginning corresponds to the initial state of the stack (empty input).

Two copies of the source code would leave a final state of

[["" {: ".~"]} ".~"] {: ".~"]} ".~"]

and so on.

What happens next

After executing the source code, the interpreter does the following:

  1. It wraps the entire stack in an array, and pushes that array on the stack.

    For a single copy of the source code, the stack now contains

    ["" {: ".~"]} ".~"] [["" {: ".~"]} ".~"]]
    
  2. It executed puts with the intention of printing the wrapped stack, followed by a linefeed.

    puts is defined as {print n print}, so it does the following.

    1. print prints the wrapped up copy of the stack without inspecting it (i.e., without converting it to its string representation). This sends

      {: ".~"]}.~
      

      to STDOUT and pops the stack copy from the top of the stack.

      The stack now contains

      ["" {: ".~"]} ".~"]
      
    2. executes the code block we defined previously.

      : begins by saving ["" {: ".~"]} ".~"] in the space character, then ".~" pushes itself and ] wraps the stack in an array.

    3. n pushes a string consisting of a single linefeed.

      The stack now contains

      [["" {: ".~"]} ".~"] ".~"] "\n"
      
    4. is executed once more. However, it was redefined when we called it for the first time and now contains an array, not a code block.

      In fact, it pushes ["" {: ".~"]} ".~"], leaving the stack as

      [["" {: ".~"]} ".~"] ".~"] "\n" ["" {: ".~"]} ".~"]
      
    5. Finally, print prints the topmost stack item without inspecting it, sending

      {: ".~"]}.~
      

      to STDOUT.

Dennis

Posted 2016-01-15T23:31:32.083

Reputation: 196 637

1

, 26 chars / 36 bytes

⟮ô`\u27ee⦃ᶈ0}\u27ef
`ď2)⟯

Try it here (Firefox only).

Note that there is a trailing newline.

Explanation

Standard quine: ⟮ⒸⅩ222+ᶈ0

Modifications:

  • Use ô function to output the quines all results instead last result (as done using implicit output)
  • Use \u27ee⦃ᶈ0}\u27ef\n and close copy block to prevent conflicts with later copy blocks.
  • Use ď2 to repeat string twice.

Mama Fun Roll

Posted 2016-01-15T23:31:32.083

Reputation: 7 234