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

marked as duplicate by AdmBorkBork code-golf Oct 12 at 19:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

11 Answers 11

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.

V, 3 bytes

ÀII

Try it online!

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

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.

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.

  • Could you provide an explanation? – FireCubez Oct 12 at 14:00
  • 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) – Titus Oct 12 at 14:53
  • I'd have to include -r in the byte count, don't I? – oktupol Oct 12 at 15:02
  • 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. – Shaggy Oct 13 at 0:24

Perl 6, 25 bytes

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

Try it online!

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

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!

  • 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')") – FireCubez Oct 12 at 13:40
  • Yes it is... it just outputs console.log("hello") – FireCubez Oct 12 at 13:41
  • Does not work for n=0 – FireCubez Oct 12 at 14:00
  • Nevermind, it's just 1-indexed – FireCubez Oct 12 at 14:01
  • 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) – ETHproductions Oct 12 at 18:52

Haskell, 42 bytes

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

Try it online!

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

Jelly, 2 bytes

Ṿ¡

Try it online!

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

Python, 55 bytes

c=lambda s,n:c("(lambda:"+repr(s)+")()",n-1)if n else s

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!

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