The Power Of Functional Languages

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.

One thought on “The Power Of Functional Languages”

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax