Friday, September 11, 2015

FUNctional Programming


What is functional programming? What is the difference between this new topic and other programming languages? Why the hell do we need to learn this functional programming? What is this logo talking about?

If this is the first time you hear this concept, you may have the above questions in your mind as I encountered for the first time I heard about "FUNctional Programming" (FP). As a rule of thumb, I don't want to go through the details of FP in this post, but I just want to attract your attention to this new term and answer the aforementioned questions briefly.

Here is the first answer! (without knowing what the question is).

So if you are still with me and would like to learn FP too, first look at the following taxonomy to see the classification of programming languages,

More likely, you never implemented any code in none of these languages, so determination of the type of your favourite programming language is more than welcome in the comments (this is subtle way to know if you read this post, if so you should be able to identify what the type of R is).


This is the first moral lesson we should learn from FP, "war is over", oops, I'm sorry! we have already been overwhelmed in many wars nowadays. Where were we? Aha, I just remember, FP says declaring var is over! But why? Why is it important to have this as the life motto of FP? This is what I will answer later. Let's first answer the vital question, so what on earth is FUNctional Programming? I think, it is better to be patient, if you know what FP is, then please move to the next post and if you have not known it yet, it means either you don't need to know it at all or read the rest of this post!

FP is a list of things you can't do. That's the most simple answer I have for you right now. In fact we can do many things, but we may never think what we can't do, now you can cry. At least this is more convincing for me to know what I can't do rather than knowing what I can. From the information theory point of view, this is more informative with the highest entropy so that facilitates my life. Just a moment, what that weird list could be? There you go,
  • No Assignments (this is different from your homework!).
  • No Varying Your Variables. (Immutable)
  • No While/For Loops.
  • No Control Over Order of Execution. 
  • No Side Effects.
  • No Mutating or changing state.
Characteristic
Imperative approach
Functional approach
Programmer focus
How to perform tasks (algorithms) and how to track changes in state.
What information is desired and what transformations are required.
State changes
Important.
Non-existent.
Order of execution
Important.
Low importance.
Primary flow control
Loops, conditionals, and function (method) calls.
Function calls, including recursion.
Primary manipulation unit
Instances of structures or classes.
Functions as first-class objects and data collections.
Looks cool! Sounds like I don't need to write any code in this fashion! But wait a moment,

Are You FUNing Kidding me? How Can Anyone Program like this ?
Good question! (this phrase reminds me someone)

"Functional Programming is so called because a program consists entirely of functions." 
- John Hughes , Why Functional Programming Matters

What a ridiculous quote! I thought a program consists of coffee :D

Now words of wisdom 
"The Language that does not affect the way you think about programming, is not worth knowing." 
- Alan Perlis

Hmmm, this one makes more sense! At least it saves my time not to learn a languRage (this is not typo! (Can anyone tell me how many exclamation mark did I use so far?!)).

But let's dive into an example,

Imperative

total = 0
i = 0
while i <= 10
  total += i
  i = i + 1
end
total

Functional

 total = (1..10).inject(:+)

I prefer to stop at this point, but I will continue this tutorial based on you feedbacks in comment section (I promise to write the next one with more appropriate syntax and semantic in a classy way), in addition to the answer of question that was raised in this post.

Have a FUNctional programming ;)

2 comments:

  1. Hi Sajjad, It is odd to me that we do not have control on the order of execution! I believe at least each line of command is executed in order.

    ReplyDelete
    Replies
    1. Actually this is the Lazy evaluation feature which takes the advantage of no side-effect to keep the order of execution arbitrary which is useful for parallel computing.

      "A function call can have no effect other than to compute its result. This eliminates a major source of bugs, and also makes the order of execution irrelevant — since no side-effect can change an expression’s value, it can be evaluated at any time. This relieves the programmer of the burden of prescribing the flow of control."
      https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf
      More good answers could be found here,
      http://stackoverflow.com/questions/23982259/what-is-order-of-execution-and-how-does-it-affect-code
      http://www.rajeevnb.com/2015/06/04/functional-programming/
      http://caml.inria.fr/pub/docs/oreilly-book/pdf/chap4.pdf

      Delete