Function Composition and Programming
In high school math, we might have seen functions that are written in the form of $g \circ f$. Which is just\[(g \circ f) (x) = g(f(x)).\]
(a full definition is, if $f: X \to Y$ and $g: Y \to Z$, then $g \circ f: X \to Z$ – so that we may exclude $x$ from the description)
In programming (especially functional), we often passing values over functions continuously. That is we take an output from one function to feed in as an input to other function in the chain.
In other words, with this code.
first_input = x first_output = f(first_input) second_input = first_output second_output = g(second_input) y = second_output
Or reducing everything to save variables/functions naming.
y = g(f(x))
The question is, if we want to declared this chain of functions without applying the argument right away. How can we do that?
One way to do that, by using lambda.
h = lambda x: g(f(x))
So now we may call this later.
y = h(x)
Simple thought, but why use
In imperative languages, there might be no other choices. But for functional languages like Haskell, we can just write this.
let h = g . f
It is just $g \circ f$ that we’ve seen before! And just call it with this.
We might also abandon $h$ completely when we want to run it right away.
(g . f) x
Wow… This is mathematics!!