# Mindshift: Part 7

# Ordering a Takeaway

## Subtraction, That Is, Not Fish & Chips…

In the previous Mindshift blog, we looked at how minimal JavaScript arrow functions can be used to define firstly a `PAIR`

of values, and secondly how extracting the first or second item from such a `PAIR`

yields a definition of the Boolean values of `TRUE`

and `FALSE`

.

So now that we have these definitions under our belt, we can look at how subtraction is implemented.

In the same way that the addition of `a`

and `b`

is defined as the `b`

’th successor of `a`

, so subtracting `b`

from `a`

amounts to nothing more than finding the `b`

’th predecessor of `a`

.

So the task for this blog is to develop a `PRED`

ecessor function (see footnote 1) and then from that, develop a `SUBTRACT`

function.

## Counting Backwards, Or Not…

First however, we must understand the constraints of our current counting system.

- We always start counting from zero
- We can only count upwards. I.E. this system provides no way to represent negative numbers

This does restrict the functionality of our `SUBTRACT`

function, but that’s not an issue here for two reasons:

- We said we are only going to define the counting numbers, and by definition, these are the positive integers.
- Our primary focus is to understand the use of abstract functions, not create a full mathematical toolbox.

So given that our implementation cannot actually count *backwards*, we will have to think of the predecessor of a number in a different way.

The way we do can this is to think of numbers as pairs such as `(n-1,n)`

. Then, to derive the predecessor of a number, we start counting up from zero – but always keeping track of the numbers as pairs. This means that when we’ve finished counting up to `n`

(the second number in our pair), the required predecessor will be the first number of the pair.

In the previous blog, we saw how the `PAIR`

function operates, so now we need to apply that function to our situation.

## Counting in Pairs

What we need now is a function that creates not just the successor of a single quantity function, but creates the successors of a `PAIR`

of quantity functions. So what we need here is a `SUCC_PAIR`

function.

Here, we will implement the “*shift and increment*” principle by which a new pair is constructed as follows:

- The first number in the new pair is the second number from the old pair
- The second number of the new pair is the successor of the second number of the old pair.

Now let’s use this function to create the next two successor pairs to `PAIR(ZERO)(ZERO)`

– which we expect to be `PAIR(ZERO)(ONE)`

and `PAIR(ONE)(TWO)`

.

## When Did Last See Your Father – Or Any Other Predecessor?

Lastly, to find the predecessor of any particular number `n`

, we need to pass the quantity function of magnitude `n`

the transformation function `SUCC_PAIR`

and a starting value of `PAIR(ZERO)(ZERO)`

. The required predecessor will then be the first item in the resulting pair.

Phew! That was a lot of hard work just to create a predecessor function, but finally we’re in a position to define a `SUBTRACT`

function.

## Subtraction Engine

Since subtraction is non-commutative, we must stipulate that `SUBTRACT`

always takes the second quantity away from the first; but apart from that, we’re using exactly the same principle as has been used before. In other words, to subtract `b`

from `a`

, apply the `PRED`

function `b`

times starting from `a`

.

Now that we have the ability to perform any subtraction whose answer is greater than or equal to zero (see footnote 2), we can start to look at other arithmetic operations such as modulus and division; but, as always, there’s a catch – in fact there are two catches caused by the fact that both the modulus and division operators are implemented using repeated subtraction:

- We must perform the
`SUBTRACT`

operation until the divisor becomes bigger than the remainder – yet, we have no functions such as`IS_ZERO`

or`IS_LTE`

(is less than or equal to). - Then there’s the thornier problem that we have no idea how many times the
`SUBTRACT`

function needs to be called. In such cases, we must use recursion.

Teeny weeny problem though… Our ground rules state that all functions must be anonymous. Therefore, how can we get`SUBTRACT`

to call itself if it's not allowed to know its own name?

This is where something called the Y-Combinator comes in; but more of that later.

Chris W

All source code from the Mindshift blog series can be found on GitHub

### Footnotes

The definition of the predecessor function shown in this blog is not the only way this functionality could be implemented, but it is the simplest to understand. The following definition of the predecessor function is also widely used:

`var PRED = n => f => x => n(g => h => h(g(f)))(y => x)(y => y);`

Whilst this definition is equally valid, it is much harder to understand because it involves the use of nested wrapper functions – so we won’t look at this implementation here.

It might seem ridiculous that we have created a

`SUBTRACT`

function that yields zero when asked to calculate`5 - 8`

.

But this is not so wacky as it might seem. For more information on this type of operation, look up Monus – but be warned – you’ll be venturing into the land of Abstract Algebra and Group Theory.

Here be dragons.