exupero's blog

# Chunking stack-math operations

When using stack algorithms to do mental math, some sequences of operations are easier to think of as single steps. For example, to halve the number on the top of the stack a computer requires the divisor to be pushed for the dyadic `/` operation, but to a human, halving is a monadic operation.

This is partly chunking, but it also reflects the difference between how the human brain does math differently than a computer processor. A computer multiplying or dividing by ten performs several non-trivial steps, while a human only moves a decimal. We think in a notation that is optimized for certain operations, which eases the mental burden. In other cases, we've just practiced the operations enough to make them second nature.

Below are the operations I tend to think in when doing mental math with stack algorithms. We'll use these in the next post and the final post of this series as our main vocabulary.

``````(defmacro defalias [nm ops]
`(def ~nm
^:stack-op
(fn [stack#]
(execute stack# ~ops))))``````
``(defalias halve [2 /])``
``(defalias third [3 /])``
``(defalias tenth [10 /])``
``(defalias double [2 *])``
``(defalias triple [3 *])``
``(defalias tenfold [10 *])``
``(defalias sqr [dup *])``
``(defalias avg [+ halve])``
``(defalias add-half [dup halve +])``
``(defalias add-tenth [dup tenth +])``
``(defalias subtract-tenth [dup tenth -])``