Showing posts with label Functional Programming. Show all posts
Showing posts with label Functional Programming. Show all posts

Sunday, January 31, 2010

Scala Programming Language – An Overview


The Scala programming language belongs to a class of programming languages known as ‘Functional Programming Languages’. Before we proceed further, let us have a quick re-cap of some the core concepts of functional programming languages, which we covered in an earlier blog post titled ‘Functional Programming – An Introduction’.

In Mathematics, ‘functions’ express the connection between parameters (inputs, in the case of computers) and the result (the output, in the case of computers) of certain processes. In each computation, the result depends on the parameters in a particular way and hence a ‘function’ is a good way of specifying a computation. This is the basis of ‘Functional Programming’.

The above notion is also more close to the ‘human world’ than to the world of a computer where in the initial days of computing, programs consisted of instructions to modify the memory, executed by the central processing unit. Thus, functional programming languages match the mathematical idea of functions.

A function is fundamentally a transformation. It transforms one or more inputs into exactly one output.

An important property of functions is that they yield no side effects – this means that the same inputs will always yield the same outputs, and that the inputs will not be changed as a result of the function. Every symbol in functional programming language is immutable.

Functional programming treats computations – running a program, solving a numeric calculation – as the evaluation of functions.

Having covered the key concepts of functional programming, let us move on to the industry scenario that led to the evolution of Scala programming language. Moore’s Law states that CPU speeds will double every 18 months. However, these days the focus is to create CPUs with multiple cores – meaning multiple CPUs within a single chip. This means that the multithreaded environment is executing on more than one CPU simultaneously as opposed to the standard ‘round-robin’ cycle executing on a single CPU. Multithreading on multiple CPUs requires that the code must be highly thread-safe. 


Attempts to resolve this problem of having highly thread-safe code has resulted in many new programming languages that addresses the concurrency problem, each language with its own virtual machine or interpreter. This obviously means that a transition to a new platform is required, similar to what happened when organizations moved from C++ to Java, about a decade ago. Such a transition is a non-trivial task and most companies consider another transition risk prone. This sets the stage for the arrival of Scala programming language.

Scala is a statically typed, object-oriented programming language. In addition to being object oriented, Scala is also a functional programming language and blends the best approaches to object-oriented programming and functional programming. Scala is designed and developed to run on the Java Virtual Machine (JVM) and Scala’s operational characteristics are same as Java’s. In fact, the Scala compiler generates byte codes that are nearly similar to that generated by Java compiler. This compatibility ensures that Scala language can utilize existing Java code, which in turn means that Scala has access to the existing ecosystem of Java code, including open-source code.

In Italian language, ‘Scala’ means stairway or steps. The name ‘Scala’ was selected to imply that Scala programming language allows programmers to ‘step-up’ to a programming environment that incorporates the latest in programming language design and at the same time letting programmers use all existing Java code. Scala also means ‘scalable language’, which means the language is designed to grow with the demands of its users.

Scala has been generating significant interest in the software industry and companies are announcing their move to Scala. Twitter, in April 2009, announced that they have switched a large portion of their backend to Scala and intend to convert the rest. Wattzon has mentioned that their entire platform has been written from the ground up in Scala.

Professor Martin Odersky is the creator of the Scala language. As a professor at EPFL in Lausanne, Switzerland, he is working on programming languages, more specifically languages for object-oriented and functional programming. His research thesis is that the two paradigms are two sides of the same coin, to be identified as much as possible. To prove this, he has experimented with a number of language designs, from Pizza to GJ to Functional Nets. He has also influenced the development of Java as a co-designer of Java generics and as the original author of the current javac reference compiler. Since 2001, Prof. Odersky has concentrated on designing, implementing, and refining the Scala programming language.

Before we conclude this discussion, I would like to quote a reference to Scala from a previous blog post, titled ‘Technology Choices for 2009 and Beyond...’ posted on 24 September 2008.
Another relatively new [first public release in 2003] language, Scala, designed and built by the team led by Prof. Martin Odersky (EPFL, Switzerland) [Prof. Odersky has also influenced the development of Java as a co-designer of Java generics and as the original author of the current javac reference compiler] also seems to be promising. On a related note, in the article titled "Java EE meets Web 2.0" written by Constantine Plotnikov, Artem Papkov and Jim Smith in developerWorks, November 2007), the authors identifies principles of the Java EE platform that are incompatible with Web 2.0 and introduces technologies, including Scala, that close the gap.

This concludes our discussion on Scala programming language, which is expected to transform software engineering, the way Java programming language did about a decade ago. 


~ Sunish

Sunday, January 17, 2010

Functional Programming - An Overview

Let us start this blog post on ‘Functional Programming’ with a widely accepted definition of computer programming – “computer programming is the process of creating a sequence of instructions which will enable a computer to do something”. Computer programming is a means to translate problems in the real world that need solving, into a format that computers can process.

Computer programming languages help convey instructions to computers. The goal of programming languages is to translate human language to machine code, the native language that computers understand.

Before we move on to have an overview of functional programming, let us have a look at the different types (or paradigms) of programming languages. Please note that a given language is not limited to the use of a single paradigm, a classic case is that of Java programming language that has elements of both procedural and object oriented paradigms.

a) Procedural Programming Languages: These languages specify a list of operations that a program must execute to reach a desired state. Each program will have a starting state, a list of operations or instructions to complete and an ending state. Two popular examples of procedural programming languages are BASIC (Beginners All purpose Symbolic Instruction Code) and FORTRAN (The IBM Mathematical FORmula TRANslating System).

b) Structured Programming Languages: Structured programming can be considered as a special type of procedural programming, which requires the program to be broken down into small pieces of code, thereby increasing readability. Local variables (local to each subroutine) are preferred over global variables. These languages support a design approach called ‘top-down approach’ in which the design starts with a high-level overview of the system. System designers then add more details to the components in an iterative fashion until the design is complete. Popular languages include Pascal, Ada and C.

c) Object Oriented Programming Languages: This paradigm is the latest and considered the most powerful of all programming language paradigms so far. Here, system designers define both the data structures and the type of operations that can be applied to those data structures. This pair of data and operation(s) on the data is known as an object. A program can then be viewed as a collection of objects, which interact with one another. The important concepts associated with the object-oriented paradigm include classes/templates, inheritance, polymorphism, data encapsulation and messaging. However, a detailed note on these concepts is beyond the scope of our current discussion. Popular languages following this paradigm include Java, Visual Basic, C#, C++ and Python.

d) Functional and Other Programming Languages: The fourth list includes functional programming and other paradigms like concurrent programming and event driven programming, which is not included above.

We will now return to the focus of our discussion – Functional Programming.

In Mathematics, ‘functions’ express the connection between parameters (inputs, in the case of computers) and the result (the output, in the case of computers) of certain processes. In each computation, the result depends on the parameters in a particular way and hence a ‘function’ is a good way of specifying a computation. This is the basis of ‘Functional Programming’.

The above notion is also more close to the ‘human world’ than to the world of a computer where in the initial days of computing, programs consisted of instructions to modify the memory, executed by the central processing unit. Thus, functional programming languages match the mathematical idea of functions. Functional programming is a new approach to solve certain classes of problems, which we will cover later in this discussion.

The main characteristics of functional programming are as below:

(a) power and flexibility – many general, real world problems can be solved using functional constructs
(b) simplicity – most functional programming languages have a small set of key words and concise syntax for expressing concepts
(c) suitable for parallel processing – with immutable values and operators functional programs are more suited for asynchronous and parallel processing

Since the concept of ‘functions’ is core to Functional programming, let us define a function before we proceed further.

“A function is fundamentally a transformation. It transforms one or more inputs into exactly one output”.

An important property of functions is that they yield no side effects – this means that the same inputs will always yield the same outputs, and that the inputs will not be changed as a result of the function. Every symbol in functional programming language is immutable.

Functional programming treats computations – running a program, solving a numeric calculation – as the evaluation of functions.

Some of the classes of problems, which can benefit from a functional programming approach, are as below:

(i) multi-core and multi-threaded systems
(ii) sophisticated pattern matching
(iii) image processing
(iv) machine algebra
(v) lexing and parsing
(vi) artificial intelligence
(vii) data mining

Advantages of Functional Programming

(a) Unit Testing: We have already noted that every symbol in a functional programming language is final and hence immutable. This implies that no function can modify variables outside of its scope and hence there are no side effects caused by functions. This also implies that the only effect of evaluating a function is its return value and the only thing that affects the return value of function is its arguments (Please see the definition of ‘function’ above). This makes unit testing much easier since the boundary values of arguments need only be unit tested.

(b) Debugging: The absence of side effects as explained at (a) above makes debugging easier since bugs are local to a function. An examination of the stack quickly reveals the cause of error.

(c) Concurrency: Functional programming does not allow data to be modified by two different threads or twice by the same thread. Hence, there is no scope for deadlocks and race conditions. This allows ease of programming in concurrent systems.

Apart from being a more appropriate tool for certain classes of computing problems, functional programming also allows programmers to make more efficient use of multi-core systems, develop concurrent/parallel algorithms easily and utilize the growing number of cloud computing platforms.

Functional programming is also considered as a means for programmers to improve their problem solving skills; it also allows programmers to look at problems from a different perspective and become more insightful object-oriented programmers as well.

Popular functional programming languages include LISP, Haskell and F#. 


~ Sunish