Function composition in Haskell
26 Dec 2012Function composition is a no-brainer concept for Haskell that makes a lot of sense. It’s truly aligned with its mathematical equivelant where you have two given functions:
The composition part comes in when you nest these functions, so that you end up with:
Mathematically, this is cake. We just nest the second function inside the first. If we look at it with respect to Haskell we end up with this:
So, the .
operator is doing the composition here for us. Below I’ve got the mathematical representation on the left, Haskell on the right.
The real power here is that you can use function composition with any two functions, just as long as the return type of the second function is the same as the argument taken by the first function.
Slick.