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:
- A fruit is put on its belt
- The machine's internals cut the fruit
- 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) receives an input and produces an output .
given the input
such that and
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:
- Pick the sliced fruit
- The machine's internals put them in a package
- 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 , a function and a second function
first
and then
or, this is better represented by
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
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:
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.