## The Little Vulgar Book of Mechanics (v0.5.0) - Functions II

Last updated: February 6th 2022

Just added a new section to the book: Functions II.

## Functions II #

Here's a collection of numbers: 2, 5, 1, 4, 3. Imagine that's all we have. Just those numbers. Nothing else. In fact, don't even call them numbers. Think of them as literally just symbols. As if you were an alien who just arrived on Earth, and has no idea what e.g. "5" means.

(Yet, conveniently for me, this alien knows about equality. Phew!)

(I don't mean "equality," as in "#Equality #JusticeForJussie." I mean the one that isn't idiotic: *Mathematical* equality, I.e. `=`

.)

Anyway, I have this collection of symbols: 1, 3, 2, 5, 4. Let's call the collection "V." We have nothing else. No arithmetic. Nothing. And I would like to have *something*, so I'm gonna start by defining a function that specifies the successor for each element. I'm gonna call it `next(x)`

, and I'm gonna define it on a case-by-case basis, like this:

`next(1) = 2`

`next(2) = 3`

`next(3) = 4`

`next(4) = 5`

`next(5) = 1`

With these definitions in mind, whenever we see an expression such as e.g. `next(3)`

, we know we can replace it with `4`

. **Complicated functions are defined using fancy clever shit called algorithms. Simple functions can be defined as just a list of equations matching each input to an output.**

Remember what I said in Functions I: Functions are like guitar pedals. E.g. `metalzone( )`

has a hole in it. You put a riff in it like `metalzone(riff)`

and you get an output. Well, here we have `next( )`

. A box with a hole. If you put "1" in it, i.e. `next(1)`

, you will get "2," because I say so (literally, I defined it.)

Notice how I didn't say `next(5) = 6`

. There is no "6" in this world. All we have is the elements in my made-up collection, V. Mathematically, we say that `next`

is a function "in the V domain." Similarly, `next(10)`

or `next("hello")`

are *not* defined. There is no "10." There is no "hello." Those expressions are, literally, *undefined*. **You are familiar with the idea of operations being undefined in math, e.g. division by zero.**

Why didn't I define the whole function in a single line, more abstractly, like this: `next(x) = x + 1`

? Because there is no `+`

either. What is "+"? I haven't defined it. There is no addition, multiplication, etc. No arithmetic. We're in the world of V. All we have is the symbols "1," "2," "3," "4," and "5." (And now `next(x)`

, which I've just defined.) **If we want addition, we're gonna have to define it.**

Before we do that, though, I want a `prev(x)`

function. You can probably guess the case-by-case definition. It'd be the same thing I did for `next(x)`

but with a different mapping of inputs to outputs. E.g. Defining `prev(1) = 5`

, so as to do the same "looping" but in the "opposite direction," so to speak.

However, could I define `prev(x)`

in a single line, instead of a case-by-case definition? And no, I don't mean by writing `prev(x) = x - 1`

, because, you guessed it: There is no subtraction in this world. At least not yet. What I'm wondering is: **Could I use next(x) to define prev(x)?**

Let's see:

`prev(x) = next(next(next(next(x))))`

Voilá! Can you see why that works? Let's try it with e.g. `prev(5)`

, replacing things with their definition, starting from the innermost expressions:

`prev(5)`

is the original expression.- which becomes
`next(next(next(next(5))))`

(by definition of`prev(x)`

.) - which becomes
`next(next(next(1)))`

(by definition of`next(5)`

.) - which becomes
`next(next(2))`

(by definition of`next(1)`

.) - which becomes
`next(3)`

(by definition of`next(2)`

.) - which becomes
`4`

(by definition of`next(3)`

.)

Note that when I say "by definition," I literally mean exactly that: Look up the definition of the function, and replace input patterns with their respective outputs. **Notice that now that we have prev(x) and next(x), we have some notion of "order" to our collection.**

(Funny. A function called `prev(x)`

, defined in terms of a function called `next(x)`

. Why did this work? Maybe it has something to do with the fact that `next(x)`

"loops," cos I defined `next(5) = 1`

.)

OK, so `prev(5)`

computes `4`

, which is intuitive, I hope. We haven't written any proofs that any of this shit works for all possible expressions. Welcome to the world of the programmer: You write shit that might break later, cos you didn't prove it mathematically, didn't test it on all possible values, etc.

We'll do proof work later. Here I just want you familiarized with the situation of starting from scratch. To have to build *everything*. **Arithmetic operations (addition, multiplication, etc.) are not some built-in "behaviors" coming "out of the box" in the symbols themselves that we call numbers.**

Notice how every single step taken consists *only* and exclusively of replacing an expression with some definition. **Computation is the replacement of expressions by their definitions, until there's nothing more to replace, e.g. a symbol like 4. Computation is rigorous search and replace.**

Speaking of arithmetic: Now that we have `prev(x)`

and `next(x)`

, is it possible to define *addition* of elements in V? Check this out:

`1 + n = next(n)`

`m + 1 = next(m)`

`m + n = prev(m) + next(n)`

There first two definitions are the obvious: "Adding one" to any number `x`

, is just another way to express `next(x)`

. And the third equation specifies what `m + n`

(for any two symbols that aren't "1") should be replaced with.

In other words, I've defined what it means exactly to use `+`

in an expression. I.e. I'm saying what to do when you see anything of the form `x + y`

in the world of V.

Let's try computing e.g. `3 + 2`

:

`3 + 2`

is the original expression.- which becomes
`prev(3) + next(2)`

(by definition of`n + m`

) - which becomes
`2 + next(2)`

(by definition of`prev`

) - which becomes
`2 + 3`

(by definition of`next`

) - which becomes
`prev(2) + next(3)`

(by definition of`n + m`

) - which becomes
`1 + next(3)`

(by definition of`prev`

) - which becomes
`1 + 4`

(by definition of`next`

) - which becomes
`next(4)`

(by definition of`1 + n`

) - which becomes
`5`

(by definition of`next`

)

So, `3 + 2`

equals `5`

? Seems legit! Commit and deploy to production!

(Just kidding, don't deploy any functions before mathematically proving them! Abort! Abort!)

(Wait, I had something for this... Oh, right, a metal track! Zander Noriega – Abort! Abort! (Abomination))

See current full book's WIP here.