I’ve been always intrigued by functional languages, learning bits and pieces of it whenever I got time, but recently I started skilling up on Haskell for building the backend systems for Makkajai.
This example below just emphasizes the power of functional programming vs imperating programming. This is an example from Learn You a Haskell for Greater Good.
“which right triangle that has integers for all sides and all sides equal to or smaller than 10 has a perimeter of 24?”
The solution to finding the answer programmatically in Haskell –
let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]
If you observe carefully, the expression just resembles the problem statement, mathematically defined. There is no logic that needs to be written, no dirty for loops that iterate and check whether the conditions are met or not – and yet haskell is able to produce a result, which in this case is [(6,8,10)].
This is the power of functional programming – most of your code starts resembling your problem statement rather than the logic to arrive at the answer. Not only is it easier to write this code, it is also easier to read and maintain compared to anything you might write in C# or Java (go ahead, try it!).
The only mainstream language that is just as powerful is SQL – which is declarative again instead of imperative. Imagine if you had to describe to the database how to fetch the data, rather than just describe the constraints and let the database do it’s job? It’s surprising that SQL is almost universally accepted but similar concept in application programming has not yet become mainstream.