Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Latest commit

 

History

History
74 lines (46 loc) · 3.78 KB

walkthrough.md

File metadata and controls

74 lines (46 loc) · 3.78 KB

Fizzbuzz Walkthrough

This is how you write FizzBuzz in Stax.

Count from 1 to 100, but replace multiples of 3 with "Fizz", and multiples of 5 with "Buzz". Multiples of both should be replaced with "FizzBuzz".

Let's start by counting to 100. Stax is a stack-based language, so all inputs and outputs use one of the two stacks.

100R{P}F

Run it

Breaking it down into parts:

100     	literal integer 100
   R    	1-based range [1..100]
    {P} 	Block of code containing a print instruction
       F	Execute block for each element in array

Now let's add some fizz and some buzz

100R{3%!"Fizz"*_5%!"Buzz"*+c_?P}F

Run it

100R{                            	For 1 to 100 do:
     3%                          	Modulus 3
       !                         	Logical not - yields 0 or 1
        "Fizz"*                  	Repeat string literal 
               _                 	Get the current iteration value
                5%!"Buzz"*       	Repeat the string logic using 5 and Buzz
                          +      	Concatenate two possibly empty strings
                           c     	Copy value
                            _    	Get the current iteration value
                             ?   	3-value Conditional; pop (a, b, c), produce a ? b : c 
                              P}F	Print as before

At this point we have a working FizzBuzz program, but it's just so big. 33 bytes. Hm. Here are some optimizations.

  • Remove the }. The F will implicitly close the block.
  • Remove the R. Iterating a block over an integer will implicitly convert it to a range.
  • Since the program ends with F, the F can be moved to the front of the block. If F is encountered outside a block, the rest of the program is treated as the contents of the iterating block

So now we have a slightly improved 30 byte program.

100F3%!"Fizz"*_5%!"Buzz"*+c_?P

Run it

Hm.

  • 100 can be replaced with AJ. A is 10, and J is square integer.
  • Instead of using an F (for) loop, use an m (map) loop, which implicitly prints its results in shorthand mode. (If m is used prior to the end of a program, it yields an array, just like a functional map)

Two more bytes.

AJm3%!"Fizz"*_5%!"Buzz"*+c_?

Run it

That's 28. Hm. We can save a little more by using compressed string literals. English-looking strings can be encoded using a different kind of string enclosed in backticks. You can use the string compression tool to automatically convert string literals.

AJm3%!`M"(`*_5%!`-C`*+c_?

Run it

That's 25 bytes. Hm. So far the program is using only printable ascii, which is kind of wasteful, since there are 256 different byte values, and printable ascii is only 95, unless you count tabs or newlines. We can convert the program to the equivalent packed-stax representation of the same program.

åS╬╕ø┤╝Φûµ╡τ╓δR╚╦>«C▲

Run it

That's 21 bytes. I don't know how to make it any smaller. For now.

There's a lot more to the language. Check out the rest of the docs.