Functional

Published By Dr. Mahsa Hassankashi | 50/03/2019

Functional Programming

Introduction

The first step in software engineering is to have a good knowledge of programming languages, this step leads us to select a proper language according to the specific scenario. There are two main paradigms. Imperative and declarative. Imperative languages use a sequence of statements in order to accomplish a purpose.


What is Functional Programming?


There are explicit initialization and value assignments, while declarative languages use only expressions without any detailed control flow. Functional programming is a subset of declarative programming and Object Oriented Programming is a subset of imperative languages. C# and Java are OOP languages but F# and Scala are Functional languages. Although it is possible to use OOP concepts such as classes, interfaces, and inheritance in Functional languages and vice versa, for example using lambda expressions concept in C# which is a Functional language.



        //Imperative
        string[] stringList = new string[] { "Mahsa", "Kashi", "Hassankashi" };
        List oopList = new List();
        foreach (string str in stringList)
        {
            if (str.Length == 5)
            {
                oopList.Add(str);
            }
        }

        //Declarative - Functional
        List functionalList = stringList.Where(s => s.Length == 5 ).ToList();
    

Why functional programming?

The most important reason that why functional programming is on the cutting edge of technology is because of having efficient and reliable behavior when working with multiple processors or in a better word it has the best performance while working with parallel programming.

Avoiding stack overflow by Tail Call Optimization

Another reason is that loops and iterations in functional languages will be done via recursion with TCO (Tail Call Optimization). TCO is an approach to call the function without separated stack frames work and it avoids stack overflow. For example in the below code, Factorial(4) computation needs 4 separate stack frame while the next one in functional languages needs just one stack frame.

It is not TCO:

      def factorial(n):
        if n == 0:
        return 1
        return n * factorial(n-1)
    

This is TCO:

        def factorial(n):
            return fact(n, 1)
        
        def fact(n, number):
            if n == 0:
            return number
            return fact(n-1, number*n)
    

Avoiding race condition and deadlock by the aid of immutable objects as thread safe

What is race condition?

In multi-threading concept, when two or more threads try to catch a shared resource or object at the same time.

What is Deadlock?

In multi-threading concept, when process 1 hold and locked resource 2 and wait for resource 1, while exactly at this time process 2 hold and locked resource 1 and wait for resource 2.

What is Thread-safety?

In multi-threading concept, thread safe ensure that each thread can perform its execution properly in simultaneously way without unintended interactions.

What is Immutable?

Immutable object`s state or value can not be changed after creation. a is mutable, because its value will be changed:

a = 1

b = 2

a = a + b

In creation time a should be defined as read only and immutable to prevent change its value.

Conclusion:

Because of immutable as thread safe in functional programming; therefore it is useful for infinitive for or foreach loop - special in mathematic functions - and prevent race condition and Deadlock.






Join Us

Let's subscribe and follow our daily tutorial about technology. You can order your difficult topic and see on subscriber your topic with your name.