In this Blog we will be discussing the basics of functional programming ,its need and characteristics and will focusing on the reasons which has made Scala popular.
We also will be showing the detailed explanation for installation of Scala and execution of the first Scala application using both interactive shell and Integrated Development environment(IDE).
100% Free Course On Big Data Essentials
Subscribe to our blog and get access to this course ABSOLUTELY FREE.
Let’s kick start our discussion by putting some light on the core characteristics of Functional programming.
What is Functional programming?
Functional programming is a paradigm in which everything is treated as a computation of mathematical functions and avoids changing state and mutable data. Here programming is done with expressions. Everything is done in the form of expressions like evaluating the expression, and the value obtained with the expression is returned as a result, rather than changing the state of the data.
In functional programming language, the output of a function depends on the arguments that are provided as inputs to the function. So if you call a function ‘f’ ‘n’ number of times with the same argument ‘x’ will result in the same output f(x) every time.
The Programming paradigm is named as Functional programming because everything is carried out in functions, its primary operation or functional operation is the application of functions to arguments.
Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungs problem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus. Another well-known declarative programming paradigm, logic programming, is based on relations.
In functional programming language a program consists of a set of functions defined and an expression in that whose value is the output of that program.
Characteristics of Functional programming
- Functional programming languages are designed in such a way that are based on the concept of mathematical functions, everything is in the form of expressions and to effect that computation it uses the concept of recursion.
- Data in a functional programming language is immutable, which means data cannot be changed, it neither have assignment statements nor variables.
- Everything in Functional programming language is an expression, evaluating the expression and returning the result.
- No side effects, which means that when you write an expression, it is replaced by the value(result of that expression) nothing will change the expression.
- Re usability is high in Functional programming languages
- Testing and debugging can be easily performed
- Overall simplicity
- Functional programming languages can take one or more functions as inputs.
Difference between object oriented and Functional programming languages
|Object oriented programming languages||Functional programming languages|
|Good when you have a fixed set of operations||Good when you have a fixed set of things|
|Program is divided into set of object which originated from its classes||Program is divided into several functions, every function consists of expressions and return values.|
|Adding a new operation to an object-oriented program may require editing many class definitions to add a new method.||Adding a new kind of thing to a functional program may require editing many function definitions to add a new case.|
|Makes the code understandable by encapsulating the code and data||Keeps the code and data separately|
|Emphasis on modeling the data itself||Emphasis on transforming the data|
|Uses predefined methods||Uses functions in which we can define expressions|
|Ex:Java,c++,Ruby, etc.,||Ex:Scala,Haskell,Erlang,Lisp, etc.,|
Why functional programming
Functional programming languages allows to discover new way of representing programs and new way to solve the problems. The major advantage of functional programming is its side effect free functions and its mutability. Nothing can change the function. Data and functions are kept separately which will ensure that the code is not affecting the data. Functional programming languages are extensible which gives the developers ease to write their programs in a newer way in which they want.
Now let’s come to scala which has the features of both Functional and Object Oriented programming. Now we see how to install Scala in Linux.
Scala has its own interactive shell as well as it can run on IDE’s also. First let’s see how to install scala and how to use its shell.
Open the terminal and then type the below command:
After the completion of download extract the tar file by using the below command:
tar -xvf scala-2.11.1.tgz
After extracting, open the bashrc file by using the command
gedit .bashrc or vi .bashrc
and give the path of the bin folder of scala’s bin folder, you can check the path of the scala by moving into the scala directory and inside the bin directory we can use the command pwd to get the path.
Refer the below screen shot for the same.
After setting the path we need to save the file and type the below command:
The above command will sum up the scala installation.
Now we can use the interactive shell of scala by just typing scala in the terminal.
Note:Scala runs on the same JVM so java needs to be installed in your system in order to use the shell.
The scala shell looks like this
Now let us write the hello world program in its Interactive shell.
You just need to write print(“Hello World”) as shown in the below screen shot.
In the above image, we can see that Scala works with or without semicolons at the end of the print statement.
Now let us download the IDE for scala and write the same Hello World program there.
Eclipse built a separate IDE for scala, you can download the IDE from the below link depending on your system’s configuration.
Here we have downloaded Scala IDE for Linux 64 bit. By default the file will be downloaded into your Downloads folder, we can move the into our desired location. Here we are moving into the home folder. Let’s extract the downloaded file using the below command:
tar -xvf scala-SDK-4.3.0-vfinal-2.11-linux.gtk.x86_64.tar.gz
After extracting, you can see an eclipse folder in the same path. Open the eclipse folder and double click on ecplise. Refer the below screen shot for the same.
After double clicking on eclipse the eclipse IDE for scala will open, now right click anywhere in the IDE and create a scala project as shown in the below screen shot
Now a scala project will be created, as we already specified that scala runs on the same JVM, it uses the JRE only. Now we will create a scala object and then we will write the Hello world program.
Now a scala object will be created, in the object a main class is to be written
def main(args:Array[String]) is the syntax for main method in Scala. Now write the code in the main class. Here we are writing the program to print “Hello World”.
Now after writing the program Right click on the screen and click on Run As and then click on Scala Application
You will get the output Hello World.
In the series of Functional Programming, in our next blog we will be covering Basics of scala i.e., Scala REPL, commenting in scala, semi colon inference, keywords that are present in scala, packages and how to import them, Declarations and finally the Data types.
Hope this blog helped you in understanding functional programming and how to install Scala and how to run a hello world program in Scala. Keep visiting our website Acadgild for more updates on Big Data and other technologies. Click here to learn Big Data Hadoop Development.