ThoughtsOfChains

Functions

It's a brand-new day at the fruit-slicing 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 fruit is put on its belt
  2. The machine's internals cut the fruit
  3. The sliced fruit comes out

The fruit is an input to the machine. The machine performs an internal process and then outputs a sliced fruit.

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 packaging of the sliced fruits, how can our machine do that? With another machine! The company just bought a new packaging machine that will:

  1. Pick the sliced fruit
  2. The machine's internals put them in a package
  3. The packaged sliced fruit comes out

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 packaging machine receives some sliced fruit (orange, apple, ...) and a type of package (jar, tray, ...). For each combination of fruit and package, it must perform its task accordingly:

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

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

This is fundamental for computer science.

But what if we want to create functions that can solve much more complex problems? What if, instead of simply adding two numbers, we wanted a function that could understand and generate language? Or classify an image? Or recognize objects in a photograph? How do we create functions for problems where we don't know all the rules ourselves?

That's where AI and machine learning come in.