Skip to content

Reading of Functional Programming papers and articles

December 26, 2014

Its not that I am against variability in coding styles. In fact I welcome them. Like to switch between single file long programs. To modular programs where there are other people involved. Also I make no claims to know-it-all-ry or any kind of programming style chauvinism.

I have been foolish enough to consume the Objected Oriented (OOP) snake oil, and been a salesman as well. So no claims to innocence or purity either.

But I do enjoy programming a lot and am a enthusiast, at a personal level also. Happens to be my profession as well, so it doesn’t hurt. Keep making efforts to learn and update myself on algorithms. A big fan of Memoization (or Dynamic programming). Favorite category of programming in hackerrank (earlier topcoder).

I claim competence in C, C++, Java and Golang on the server side languages. And can work on the client side too, but often with lesser enthusiasm.

So with that kind of a background. Whenever I see an article like Ten technical papers every programmer should read, you can understand its difficult for me to ignore. I saw this on HN few days back, and so decided to give a go to Why functional programming matters.

I learnt few things from the paper. Paraphrasing my understanding in very crude terms:

1) Recursion – Whether its a list or a map. You can use recursion to do things like sum or multiply or square or apply a function on all items.
e.g. Fact(i) = i * Fact(i-1)

2) Functional programming offers you some new kind of “glues” to modularize your program.s, in different dimensions from declarative programs. ‘

3) Lazy computation – Let there be two functions f(x) and g(x). You can do things like f(g(x)). And g(x) does not start to compute upfront. g(x) starts to run when f(x) first need the input, and is aborted as soon as f(x) needs no more inputs. So the idea being that only the needed portions of g(x) are run, and only if its needed. This idea definitely seemed of a lot of merit to me, and missing from the conventional languages we use – C/Java/C++. In fact this is the only idea which appealed to me over all.

Read some other interesting articles like why the above referred to paper matters. But honestly I could not buy into the telephone test example he gave. Its too simplistic, and also the idea is not profound. Also when I read some other articles, they seemed contradictory. For example see the Erlang examples in this one.

Then again I read (skipped some portions) in this one by a perl guy. It may seem old, written in 2005. But please not that FP concepts itself date back to 70s and 80s, and its a fairly deep review. Also read the comments on that one, some of them are very high quality like this one.

Reading this set me off in a humorous mood. And I began jotting down some humorous takes on FP. Please note that I am not totally anti-FP. But I consider myself to be bitten by OOP, with my history of having both consumed and being a salesman mentioned above. Especially the Java kind. And in the present state, with more of the programming done in Golang, have developed a healthy dislike and contempt for its supposed gains.

FP Humor quotes:

1) The Only difference between OO and FP snakeoil salesmen, is that the former’s BS has been tried out. Extensively.

2) The quanta jump we got in productivity from assembly to C. Was never delivered again by OOP. FP firmly remains a wet dream.

3) I think OOP and FP are just opinions on how things should be done. On how best to get whacked on the bum.

4) N N Taleb on FP programmers: Failed mathematicians; no-skin-in-the-game anti-pragmatists; charlatans; pretending to do algebra on a computer.

5) The only thing which are stateless are your minds, as a computer doeth have state

6) And the only thing lazy are your fat bums, as CPU slogs away to unravel all the nothingness between your curly braces.

7) Go take a class on Memoization( or DP), and do some real structured programming in a scalable language.

8) Algorithms should be **fast**. Not some lazy evaluating Jack assery. And Tail recursion Orgy.

— Humorous quotes end folks. No offence, just for laughs —-

Finally I read a relatively less deep blog. But that gave me a sense of FP draw backs. Although its heavily pro FP.

It says:

The one problem with recursion is that performance is not very good. It is also susceptible to stack overflows if not properly implemented. Luckily, certain languages such as Haskell and Scala can generate code that represents a loop or eliminate the necessity to produce a new stack frame with every recursion. This is known as tail call optimization and can be employed when the recursive call is the last statement in the function

And I went, what. malloc and free are dangerous as somebody can shoot themselves in the foot. But following this tail recursion – compiler dependent madness is good?!

Also when I looked at the tree analogy in the same article, I thought. What so in f(g(x)) kind of call how are we going to maintain immutability of the tree? Are we going to return the entire new tree by value? I know compilers will be smarter. But this question is still unanswered in my mind properly.

Also as some of these articles say. Now gradually things like Closure functions are creeping in all languages. Golang has it! Javascript had always had it. Also you get styles to apply functions iterating over a list or a map in various languages.

Also if you are past that OOP madness the functions have been set free! So honestly in a post-OOP world, some nice FP principles may already have been subsumed in a similar kind of way. For example Golang has a concept of channels, and I can spawn of any piece of code in a named or anonymous function by just prefixing it with ‘go’ and it runs concurrently.

So in that state of things, I am not sure, how much a pure FP style is relevant. Particularly when its not easy to teach. And various claims that its not maintainable. And there are numerous stories of people trying it out and then dropping it. Two recent (recent some years) examples are Cleartrip and Reddit. Both tried in some language like Lisp, but then shifted onto some other language like Java in one case and Python in another.

At the same time I do know that ITA software the company acquired by Google few years back, had there core engine in Lisp. It would be interesting to know how is the experience since then.

Also I have not heard of any popular applications or software, which has been developed in a language like Lisp or similar. Although I know that Emacs can be customized in Lisp, but what was it written on? I don’t think it’s Lisp.

Also none of the big companies like Google, FB, Amazon are known to use Functional programming. Must add the Whatsapp was done in Erlang. But I suspect, it was more because if it being a highly concurrent language and tried and tested in Telcom companies. Whatsapp is a telecom app, so to speak.

So I don’t know, if I see any real merits in basing ones application using a pure functional programming approach. There are elite people who do claim to have a pristine ‘aha’ while coding in it. But I must add, in the mid-late 90s OO interfaces and pure abstract classes were perhaps giving the same orgasmic feeling to a lot of converts then.

But in the past few years we realized, that bulk of the gain is got by code being modular and honoring contracts, and sturdy and reusable and so on. Fetish for one kind of purity, always leads to just shifting some messy pieces some where. Finally the devil lies in the details!

Time will tell, how right or wrong we all were. Just thought should capture this thought, to remember more.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: