ThoughtsOfChains
← Fundamentals

Functions

It's a brand-new day at the cube-painting factory! But, unfortunately, the company just bought a new machine that does the work much faster. The process of using this machine is simple:

  1. A cube is put on its belt
  2. The machine's internals paint the cube
  3. The painted cube comes out

cube painting mahine

The cube is an input to the machine. The machine performs an internal process and then outputs the painted cube.

This simple example illustrates the concept of a function: a mathematical representation of a “process”. Functions are broad and can be used to describe a wide variety of things:

seed -> plant(seed) -> tree
dough -> bake(dough) -> bread
...

Or, more generally, in mathematics, a function (or process) ff receives an input xx and produces an output yy.

f:X→Yf : X \rightarrow Y

given the input xx

f(x)=yf(x) = y

such that xx ∈\in XX and yy ∈\in YY

Composition

But now, imagine that we want to upgrade the simplicity of our process with automated texturing of the cubes, how can our machine do that? With another machine! The company just bought a new texturing machine that will:

  1. Pick the painted cube
  2. The machine's internals drill a texture in it
  3. The painted and textured cube comes out

machine diagram with painting machine and texturing machine

By adding this new machine sequentially after the first one our product just became better, and, talking about functions, this means that functions can be used for composition:

given the input xx, a function ff and a second function gg

first

f(x)=yf(x) = y

and then

g(y)=zg(y) = z

or, this is better represented by

g(f(x))=zg(f(x)) = z

Notice that g(f(x)) means “apply f first, then apply g to the result.”

And compositions are really powerful. A big problem can be broken down into a sequence of smaller and more specialized functions, like a real factory!

Want to build an airplane? There's a bunch of steps that need to be made.
Gather the material, Do a blueprint, Create the parts, assemble, do tests and then fly

or written in more function-like notation:

fly ( test ( assemble ( create ( blueprint ( gather ( idea ) )))))

A similar process can be made with functions

f(g(h(z(x))))=yf(g(h(z(x)))) = y

It's better to specialize at each step rather than try to do it all together

Multi-variables

Functions can also take more than one input.

Imagine that our machine receives some paint color (orange, blue, ...) and a type of object (cube, sphere, ...). For each combination it must perform its task accordingly:

f(x,z)=yf(x, z) = y

machine composition The number of inputs can theoretically be infinite, but in practice, functions usually have a finite number of inputs, which can vary depending on the task.

Coding

Most programming languages take advantage of the concept of functions because they provide abstraction and make code more reusable, speeding up the development process.

If you want to add two values, you can simply create a function and reuse it as many times as needed:

function add(a, b):
    return a + b

even composition is easy to use

function sub(a, b):
    b = -b
    return add(a, b)

This is fundamental for computer science.

But what if we want to create functions that can solve much more complex problems (understand and generate language, classify an image, ...) or solve problems where we don't even know all the rules ourselves?

That's where AI and machine learning come in.