Just few days ago , I started to learn SCALA. Scala is pretty new language to the programming arena. Not many people using this language right now. But I found it very interesting to work with it. It has the power of functional programming and object orient programming. Technically, Scala is a blend of object-oriented and functional programming concepts in a statically typed language. Though it has the disadvantage of statically typing language , it provide a powerful and concise way to write applications. You can apply Scala to a wide range of programming tasks, from writing small scripts to building large systems. Scala is easy to get into. It runs on the standard Java platform and interoperates seamlessly with all Java libraries.

     If you want to install scala from linux it is enough to type ‘sudo apt-get install scala’ from terminal. Also you have to install java if it is not there.

A simple example with scala interpreter:

I would like to show a simple example that shows you how scala program really interpret its input. In order to work small example use scala interpreter by typing ‘scala’ from terminal.

 jinesh@jinesh-Compaq-Presario-CQ40-Notebook-PC:~$ scala
Welcome to Scala version 2.7.7final (OpenJDK Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> 1+2
res0: Int = 3

really here ‘+’ is not an operator. In scala every operation is function call. when you type 1+2 what actually happens is (1).+(2)

‘1‘ calls function + with arguement 2, and the + function is predefined to calculate the sum of its input.Thus, when you typed 1 + 2 into the Scala interpreter in Step 1, you were actually invoking a method  named + on the Int object 1, passing in 2 as a parameter.

 Let’s look another example.

The following function returns the largest of two numbers.

scala> def max(x: Int, y: Int): Int = {
if (x > y) x
else y
max: (x: Int,y: Int)Int

When contrast to other languages scala defines type of variables after the variable name.  Also the return type is specified just after parameter list braces.


If you want to write a script just make a file with ‘scala’ as extension (eg: ‘filename.scala’). Inorder to run type ‘scala filename.scala’.

The following script read an input string from terminal and print it on the console.

// save this script as echoargs.scala
var i = 0
while (i < args.length) {
if (i != 0)
print(" ")
i += 1

if you run this script it will produce the following output.

‘scala echoargs.scala scala is more fun’

output: scala is more fun

But this is not the way of functional programming. Infact this is our old way of approaching in statical way. You can implement the same problem in a different and concise way using ‘foreach’.

args.foreach(arg => println(arg))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s