Folding functions

Kapitoly: What is a function, Functions of multiple parameters, Folding functions, Graph a function

One feature won't do much. That's why functions are allowed to be composable.

Motivation

Let's have two functions. For example, a function that calculates the number of kilometres driven per litre of petrol. Suppose our car travels 10 kilometers on one liter of gasoline. We label this function f and define it as f(x) = 10x, where the parameter x denotes how many liters of gasoline we have. So if we have six litres of petrol in the tank, the car will still travel f(6) = 10 · 6 = 60 kilometres.

The second function can calculate how many litres of petrol we can buy for a given number of crowns. Suppose a litre of petrol costs 30 crowns. Then the function, let's label it g, would look like this: g(x) = x / 30, where x indicates how many crowns we want to buy the petrol for. If we have 150 crowns, we would buy g(150) = 150 / 30 = 5 litres of petrol.

Now we could ask, how many more kilometres will we drive if we buy petrol for 750 crowns?

We already have functions that always calculate a partial thing - the number of kilometres driven versus litres, and the number of litres of petrol bought versus money. Now we would need to add both functions together.

How to solve this

We could solve it by first calculating how many liters of gasoline we would buy and then how many kilometers we would drive. Let's try this. For 750 crowns we would buy g(750) = 750 / 30 = 25 litres of petrol. And on 25 litres of petrol, we would drive f(25) = 10 · 25 = 250 kilometres.

But we can solve it another way, by stacking functions. We can define a new function h, which will take the number of money as input and return the number of kilometers traveled as output. We define the function h using the functions f and g as follows:

$$ h(x) = f(g(x)) $$

The function h defined in this way actually does what we calculated in the first paragraph. It tells us that when we get the value of x, the number of money, we should first calculate the value of g(x), which is the number of liters that the money buys. This resulting value g(x), so in our case g(750) = 25, we have to put into the function f, so we still calculate f(25) = 250.

We can also get a direct notation for the function h. We take the function f and replace all occurrences of the parameter x with the function g. The function f looks like this: f(x) = 10x and we substitute the function x / 30 for the parameter x. This gives us the function h:

$$ h(x) = 10\cdot\left(\frac{x}{30}\right) $$

Edit/Reduce:

$$ h(x) = \frac{x}{3} $$

We have now folded the functions f and g into the resulting new function h, which calculates how many kilometers we drive for gasoline worth x crowns. We can check this by putting the good old 750 crowns into the function:

$$ h(750) = \frac{750}{3} = 250. $$

The result agrees with our previous calculation.

Folding more complex functions

Let's have the functions f and g defined as follows:

$$\begin{eqnarray} f(x) &=& \sin(x) \cdot x^2 - 4\\ g(x) &=& \frac{x+1}{x-1} + 5 \end{eqnarray}$$

Let's now try to fold the functions as follows: f(g(x)). So we take the function f and instead of all occurrences of the parameter x we insert, preferably in parentheses, the function g:

$$ f(g(x)) = \sin\left(\frac{x+1}{x-1} + 5\right) \cdot \left(\frac{x+1}{x-1} + 5\right)^2 - 4 $$

If we were to compose the functions in reverse, we would get:

$$ g(f(x))=\frac{(\sin(x) \cdot x^2 - 4)+1}{(\sin(x) \cdot x^2 - 4)-1} + 5 $$

The wheel symbol is used to fold functions: $f \circ g$. The trouble is that sometimes this notation indicates folding in the direction of f(g(x)) and sometimes in the direction of g(f(x)). The symbolism is a bit ambiguous in this case, so you should always check what is meant.