Skip to main content
5 of 5
Commonmark migration

Take advantage of the 'Stack-Height' nilad

Especially in challenges, or challenges where you always know the size of the input, you can take advantage of the 'Stack-Height' nilad [] to create integers.

Let's work through this with a hypothetical challenge: output CAT. The non-golfy way is to use the online integer golfer to push 67, 65, and 84. This gives:

(((((()()()()){}){}){}()){}())

(((((()()()()){}){}){}){}())

((((((()()()){}()){}){})){}{})

(Newlines for clarity). This is 88 bytes, and not that great. If we instead push the consecutive differences between values, we can save a lot. So we wrap the first number in a push call, and subtract 2:

(   (((((()()()()){}){}){}()){}())  [()()] )

Then, we take this code, wrap it in a push call, and add 19 to the end:

(  ((((((()()()()){}){}){}()){}())[()()])   ((((()()()){})){}{}()) )

This is 62 bytes, for a whopping 26 byte golf!

Now here is where we get to take advantage of the stack-height nilad. By the time we start pushing 19, we know that there are already 2 items on the stack, so [] will evaluate to 2. We can use this to create a 19 in fewer bytes. The obvious way is to change the inner ()()() to ()[]. This only saves two bytes though. With some more tinkering, it turns out we can push 19 with

((([][]){})[]{})

This saves us 6 bytes. Now we are down to 56.

You can see this tip being used very effectively on these answers:

DJMcMayhem
  • 59.6k
  • 17
  • 196
  • 348