2
\$\begingroup\$

Input

A string s of printable ASCII characters, newlines, and spaces (0x20 ( ) to 0x7E (~)), and a non-negative integer n.

Challenge

To write a program that outputs either another program, or s depending on n. If n = 1, Then your code should output code which outputs s. If n = 2, Your code should output code which outputs code which outputs s and so on.

All outputted code should be in the same language as the language of the original submission.

Test Cases

Format: s, n -> output Assuming your submission is written in JavaScript:

No cycles, 0 -> No cycles
Hello, 1 -> console.log("Hello");
Cycle!, 3 -> console.log("console.log(\"console.log(\\\"Cycle!\\\")\")");
:), 3 -> console.log("console.log('console.log(`:)`)')");

Scoring

This is , so aim for shortest code in bytes.

Additional Rules

  • I/O format is flexible
  • Default rules and standard loopholes apply.
  • The output of your submission must be valid.
  • You can use any language, even if it was created after this challenge, as long as it wasn't created for the purpose of this challenge
  • This isn't a polyglot challenge. A JavaScript submission shouldn't output Ruby code.
  • Input is guaranteed to be valid as defined in the Input section

Validation

Here's a quick tip to test your answers:
For n=0, output=s
For n=1, eval(output)=s
For n=2, eval(eval(output))=s
Where n and s are inputs

\$\endgroup\$
5
  • \$\begingroup\$ Are functions that output programs or functions allowed? \$\endgroup\$
    – nwellnhof
    Oct 12, 2018 at 13:45
  • 3
    \$\begingroup\$ related duplicate? \$\endgroup\$
    – Emigna
    Oct 12, 2018 at 13:46
  • \$\begingroup\$ @nwellnhof According to the default rules, yes. \$\endgroup\$
    – kepe
    Oct 12, 2018 at 13:51
  • \$\begingroup\$ @LuisfelipeDejesusMunoz Sorry, I mistyped, I meant the same language, not code \$\endgroup\$
    – kepe
    Oct 12, 2018 at 14:18
  • \$\begingroup\$ @Emigna, looks like a dupe to me. \$\endgroup\$
    – Shaggy
    Oct 12, 2018 at 19:51

11 Answers 11

5
\$\begingroup\$

R, 43 bytes

f=function(s,n)"if"(n,function()f(s,n-1),s)

Try it online!

Returns a zero-argument function that recursively calls f with n set to n-1. n successive function applications returns s.

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

V, 3 bytes

ÀII

Try it online!

Takes string as input, prepends as many I as stated in the first argument.

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

Google Sheets, 96 bytes

=Join("",If(B1,ArrayFormula("="&Rept("""",2^(Row(Offset(A1,0,0,B1))-1))),),A1,Rept("""",2^B1-1))

Inputs are in cells A1 (s) and B1 (n).

Sample Results

(Columns D, E, and F show the result if you copy the output from the column to its left and input it as a formula.)


The formula concatenates 3 pieces into a single string. The first piece is the most complicated.

If(B1,ArrayFormula("="&Rept("""",2^(Row(Offset(A1,0,0,B1))-1))),)

If If statement drops the prefix if n=0 so you end up with No cycles instead of ="No cycles. The ArrayFormula creates an array of equal signs followed by some number quotes. That number is 2^(x-1) where x counts up from 1 to n thanks to the Row(Offset(~)) combination.

The next piece (A1) adds in s and the final piece (Rept("""",2^B1-1)) adds in 2^n-1 quotes at the end.

\$\endgroup\$
1
\$\begingroup\$

PHP, 69 bytes

function($s,$n){while($n--)$s='echo'.var_export($s,1).';';return $s;}

Try it online!

From the documentation about var_export:

var_export — Outputs or returns a parsable string representation of a variable

This function calls var_export prepends echo and appends ; $n times.

\$\endgroup\$
4
  • \$\begingroup\$ Could you provide an explanation? \$\endgroup\$
    – kepe
    Oct 12, 2018 at 14:00
  • \$\begingroup\$ 1) There´s no need for a space between return and $s. 2) recursive function, 67 bytes function f($s,$n){return$n?f('echo'.var_export($s,1).';',$n-1):$s;} 3) full program, 63 bytes: for([,$s,$n]=$argv;$n--;)$s="echo".var_export($s,1).";";echo$s; (run with -r) \$\endgroup\$
    – Titus
    Oct 12, 2018 at 14:53
  • \$\begingroup\$ I'd have to include -r in the byte count, don't I? \$\endgroup\$
    – oktupol
    Oct 12, 2018 at 15:02
  • \$\begingroup\$ No, flags are free now; they count as a different language so, if you wanted to use that flag, this solution would be PHP -r. \$\endgroup\$
    – Shaggy
    Oct 13, 2018 at 0:24
1
\$\begingroup\$

Perl 6, 25 bytes

{'{Q«'x$^n~$^s~'»}'x$n}

Try it online!

Function returning function code. Uses Q« » to quote unescaped ASCII strings.

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

JavaScript (Node.js), 43 bytes

Outputs a program that outputs a program that outp.... until b=1

f=(a,b)=>b?--b?`(f=${f})('${a}',${b})`:a:''

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ This does not match the test cases, the output for f("hello", 2) should be console.log("console.log(\"hello\")") or console.log("console.log('hello')") \$\endgroup\$
    – kepe
    Oct 12, 2018 at 13:40
  • \$\begingroup\$ Yes it is... it just outputs console.log("hello") \$\endgroup\$
    – kepe
    Oct 12, 2018 at 13:41
  • \$\begingroup\$ Does not work for n=0 \$\endgroup\$
    – kepe
    Oct 12, 2018 at 14:00
  • \$\begingroup\$ Nevermind, it's just 1-indexed \$\endgroup\$
    – kepe
    Oct 12, 2018 at 14:01
  • \$\begingroup\$ Is the b=0 -> '' case required? If not I think you can save 5 bytes just by removing it. (Becomes even clearer if you use 0-indexing instead of 1-indexing) \$\endgroup\$ Oct 12, 2018 at 18:52
0
\$\begingroup\$

Haskell, 42 bytes

s#n|n<1=s|0<1="main=putStr"++show(s#(n-1))

Try it online!

\$\endgroup\$
1
0
\$\begingroup\$

PHP, 12+1 bytes, not competing

plain text is a valid PHP program, but, if N is not 0, this fails for text containing <? with short tags enabled and for text containing <?php or <?= in any case:

<?=$argv[1];

call with php -R '<code>' '<string>' <n>; call the output with php -R '<code>'

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

Jelly, 2 bytes

Ṿ¡

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Explanation? How does this wor \$\endgroup\$
    – kepe
    Oct 12, 2018 at 15:20
  • \$\begingroup\$ @FireCubez It simply executes the value → string representation function n times. \$\endgroup\$ Oct 12, 2018 at 15:31
  • \$\begingroup\$ And does executing the string representation of a value print the value? \$\endgroup\$
    – kepe
    Oct 12, 2018 at 15:33
  • \$\begingroup\$ @FireCubez The eval function (V) simply evaluates the code within a string. It doesn't implicitly print anything. \$\endgroup\$ Oct 12, 2018 at 15:35
0
\$\begingroup\$

Python, 55 bytes

c=lambda s,n:c("(lambda:"+repr(s)+")()",n-1)if n else s
\$\endgroup\$
0
\$\begingroup\$

C (clang), -DA=asprintf( -lm 136 bytes

j,*c,*e;f(a,b){A&c,"%s",a);for(;b--;){e="";for(j=1;j++<pow(2,b);A&e,"\\%s",e));A&c,"puts(%s\"%s%s\");",e,c,e);}puts(c);}

Try it online!

\$\endgroup\$

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