Stumbling on, we get to the real ‘meaty’ part of the Haskell experience - higher order functions.
It turns out that if you want to define computations by defining what stuff IS instead of defining steps that change some state and maybe looping them, higher order functions are indispensable.
They’re a really powerful way of solving problems and thinking about programs.
–Lipovaca, Learn You A Haskell
For further practice, we’ll implement the Luhn algorithm in Haskell.
You’ll notice that many of these functions are expressed without parameters, or in
“point-free” style. Does that mean these functions don’t expect parameters?
No. Parameters are still expected but due to currying we can get rid of writing the parameter on both sides.
Thanks to currying, we can use point-free style to write more concise code and take simple functions and use composition to glue them to form more complex functions.
So, for instance let’s take look at this checkSum
function:
Ugh, so many ugly nested parentheses. Let’s see if we can use function composition and point-free style to clean things up.
There much better! Clean and concise. What we just did is formally called eta reduction, where we remove the last parameter of a function if it appears at the end of both sides of an expression.
To learn more about point-free style and abstraction, here’s an excellent presentation by Haskell enthusiast, Amar Shah. Point-Free or Die: Tacit Programming in Haskell and Beyond.