Russian Nesting Quine

18

1

A Russian nesting doll, more commonly known as a Matryoshka doll, is a doll which contains a smaller version of itself, which then contains another smaller version of itself, which contains a smaller version of itself, which contains a smaller version of itself, which contains a smaller version of itself... - until finally, the last one is empty. An example:

Matryoshka Dolls <3

Today your goal is to emulate this Russian tradition by writing a program or function that, when it contains itself N times, will print itself containing N-1 copies of itself times.

For example, the doll program abcd will have the N=3 program abababcdcdcd, which will print the N=2 program ababcdcd, which prints the original N=1 program abcd, which finally prints N=0, which is empty. This should theoretically work for any reasonable value of N.

Rules:

  • Here is a TIO program to help generate doll programs based on your program
  • Standard Quine Rules apply
  • Standard Loopholes apply
  • 'Contains' means directly in the center of the previous version, so your solution must have a positive even number of bytes. A program of length 10 will have a copy of the original inserted after the fifth byte, then another after the tenth byte etc.
  • A single trailing whitespace is allowed in the output
  • As this is , your goal is to make your N=1 program as short as possible.
  • An explanation of your code would be appreciated

Jo King

Posted 2018-02-02T09:11:28.583

Reputation: 38 234

Sandbox post (deleted) – Jo King – 2018-02-02T09:12:28.407

For what N is the code size measured? – flawr – 2018-02-02T09:52:33.993

@flawr N=1..... – Jo King – 2018-02-02T10:06:43.223

N=0, which is empty it HAS to be empty, or can be a single character? – Rod – 2018-02-02T10:49:52.893

@Rod Has to be empty. 0 copies of a program is a blank string – Jo King – 2018-02-02T10:52:20.803

1Related: One More Program and I'm Out! – Kevin Cruijssen – 2018-02-02T11:15:25.853

12Is that animation really necessary?! – Shaggy – 2018-02-02T11:28:59.247

Answers

9

Underload, 4 bytes

N=1: Try it online.

 ()S

N=2: Try it online.

 ( ()S)S

N=3: Try it online.

 ( ( ()S)S)S

Explanation:

Self-explanatory, but I add it anyway.

  • (...)S prints anything between the parenthesis to STDOUT
  • The space before it is a no-op to make the byte-count even and comply to the rules of the challenge.

Kevin Cruijssen

Posted 2018-02-02T09:11:28.583

Reputation: 67 575

9

JavaScript, 36 32 bytes

Takes advantage of the fact that Function.prototype.toString() takes no arguments and will therefore ignore any that are passed to it.

Partly inspired by user202729's solution.

f=_=>f.toString( ).slice(14,-16)

Try it

o.innerHTML=["<span>Five</span>",(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16) ).slice(14,-16) ).slice(14,-16) ).slice(14,-16))(),"<span>Four</span>",(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16) ).slice(14,-16) ).slice(14,-16))(),"<span>Three</span>",(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16) ).slice(14,-16))(),"<span>Two</span>",(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16))(),"<span>One</span>",(f=_=>f.toString( ).slice(14,-16))(),"<span>Thunderbirds Are Go!</span>"].join`\n`
span{font-weight:bold;font-size:16px;line-height:1.5em;text-transform:uppercase;}span:last-child{font-size:8px;}
<pre id=o></pre>

Shaggy

Posted 2018-02-02T09:11:28.583

Reputation: 24 623

7

JavaScript (Node.js), 46 bytes

Full program. So console.log is necessary.

Use an idea from this answer to save some bytes.

l=console.log;   g=_=>{};l((''+g).slice(4,-1))

Try it online! Try it online twice! Try it online, three times!


My approach is similar to that used in Kevin Cruijssen's answer, find a nestable structure (a function in this case).

user202729

Posted 2018-02-02T09:11:28.583

Reputation: 14 620

5

Jelly, 16 bytes

Ṿḣ-9Ḋð}“““““““““

Try it online!

Doubled: Try it online!

Tripled: Try it online!


Jelly doesn't have any nestable structure, but its string literals are auto-terminated.


Ṿḣ-9Ḋ    First chain. (monadic)
Ṿ        Uneal. (to string)
 ḣ-9     Take the ead, ends at the -9'th character.
    Ḋ    equeue, remove the first character.

     ð             Terminate the first chain, start a new one.
      }            Convert the last monadic chain to a dyadic one.
       “““““““““   String literal.
                   This matches the pattern <dyad> <nilad>, so applies
                   the the corresponding rules. This way a link can take data
                   to the right of it.

Will try different approaches to see if they can be shorter.

user202729

Posted 2018-02-02T09:11:28.583

Reputation: 14 620

4

DipDup, 2 bytes

[]

It pushes the list onto the stack, and prints it without the outmost brackets.

N = 1: Try it online!

N = 2: Try it online!

N = 3: Try it online!

alephalpha

Posted 2018-02-02T09:11:28.583

Reputation: 23 988

2

dc, 4 bytes

 []p

Similar to some other answers, since strings in dc have start ([) and end (]) delimiters (that is, " doesn't perform both duties, etc.), they're nestable without any real effort. p to print.

N=1: Try it online!

N=2: Try it nested!

N=3: Try it nesteder!

brhfl

Posted 2018-02-02T09:11:28.583

Reputation: 1 291

1

Tcl, 12 bytes

puts {}     

Try it online!

It's just another language that also has this.

jimmy23013

Posted 2018-02-02T09:11:28.583

Reputation: 34 042