pages

۱۳۸۸ اسفند ۴, سه‌شنبه

Functional Programming and Erlang

Until recently, I used to believe that functional programming is some sort of an aged method and some how an old fashioned method of programming. maybe it's based on my education that I had come to this belief. From first courses of software engineering and programming I remember that there was very little emphasis on functional programming let alone functional design or even thinking. unfortunately we graduated from university having literally no knowledge of functional programming. Neither we students nor the professors were interested in it and as a result we got the impression that it is a very old and limited paradigm in programming. Even as of my knowledge, many of my classmates used to believe that languages such as Pascal and C which are not object oriented languages (though they have some object orientation capabilities) are functional languages (which are not), and since many of us didn't enjoy programming in these languages (for a variety of reasons), we were all dying to learn about object oriented languages and start programming in them. As a result, languages like Java, not only for lots of good reasons, but mainly because it was an OO language, and our savior from Pascal and C, suddenly became luxurious and fancy for everybody. I remember that whenever we heard about functional programming (whatever it meant for us) we used to say "who programs in these languages anyway?". 
Recently I've had the chance to learn a little about this subject on one of my course projects and I thought it wouldn't be bad to share my new feelings about this with my friends.
Before I start, I should mention that as I said, I've just started working on this matter and I don't have a deep knowledge in it, so if you have enough experience on functional programming, don't bother reading the rest of the post unless you want to correct my mistakes :).

For starters, as you may have noticed by now, languages like C and Pascal are NOT functional languages. More generally, using functions in a program does not make it a functional programming. "In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state" (wikipedia). So languages mentioned above (C,..), fall into imperative languages category in this classification.
In this project that I'm working on, I have a noticeable amount of concurrency and a good amount of shared state (shared variables). If you have some experience programming in Java, you might know that things like concurrency (threads), shared state, mutability, etc are things you should always work carefully with. Furthermore, if you have programmed at all, hearing the word "side effect" should definitely fire an alarm in your mind. I think these concepts make the most percentage of software faults. Interestingly, these are the exact problems that you rarely see in FP.  I'm not going to get into detailed description of FP here, but just want to share some of my experiences in this project with you.
I am using Erlang to implement a system which I've already implemented using Java and have had lots of concurrency problems doing that. Of course some of problems I faced during implementation in java was because of my little experience in concurrency paradigm.
In Erlang, there is no shared state between concurrent processes! the only method of communication between processes is message passing, so I never came to need mutual exclusion control in the code (e.g something like synchronized methods or lock objects & ..).
Program source code is much shorter than Java version. It is much faster than the Java version. Fault tolerance mechanisms are amazingly easy and straight forward, e.g you can make  process A listen to process B, and whenever B fails (any error occurs) A starts another process instead of B. and this can be done really easily compared to Java.
of course I had some problems coding in erlang, I was not familiar with the syntax at all, there is not enough tool support for it and most importantly It was hard thinking in a functional way. Generally it was a good experience and it was worth the time and effort for sure. I suggest that if you get the chance to program in a new language, try sth like erlang if it fits your problem. Anyway, If anyone is interested I can prepare some code snippets and write more about Erlang and FP. But for now I think I have shared my good feelings about FP and Erlang with you ;)

۴ نظر:

  1. bah bah che matne zibaa va jazzabi bud :d
    dastetun dard nakone be darde man ke kheili khhord :-p

    پاسخحذف
  2. Excellent post dude! But I didn't get the difference between the Erlang and Java's concurrency model. It would be nice ifyou put two code samples in your next post and implement a simple behaviour with these two languages.

    پاسخحذف
  3. to wickoo
    Hi Ali and thanks for your comment.
    I'll do what you asked for sure, maybe during next week (because I'm busy with my university projects this week). but for now, about the difference between the concurrency models, I should say that:
    the dominant difference in the models is that in Java you have some data as vairables or object and some threads which access them. and some of the these objects (state) are shared between threads (the problem comes here). but in erlang there is no way to share the state between processes as shared objects. the only method of communication between threads (processes) is message passing. and messages are all values. one might think that this method doesn't have the same strength as shared variables, but it is proved that they are equally strong. there is an interesting matter called higher level functions in which a function can be passed as arguments to other functions. using this method lots of interesting stuff can be done.
    I'll try to create some samples here whenever I can.

    پاسخحذف
  4. منم باید خارجی کامنت بذارم یعنی؟
    با علی موافقم. یه کد اگه بذاری خیلی بهتر میشه فهمیدش. خیلی علاقمند شدم. دستت درد نکنه

    پاسخحذف