Welcome to my 5-part Scala tutorial. This is meant as an excercise in Scala for beginners and doesn’t require any knowledge about programming. The difficulty will increase from part to part, and hopefully you will be able to solve lots of problems on your own with Scala in the end. Enough chit chat, let’s get started!
- The Scala environment. I recommend programming in Ubuntu linux. The best thing to do would be to get the newest installer from the official website
- A trusty text editor. I use Gedit in Ubuntu.
- A good cup of coffee
Starting with REPL
Okay, now you’re ready to begin programming in Scala. There are two ways to work with scala. One way is to use the interpreter, or so called REPL (Read-Evaluate-Print Loop). In this way you can write and test your program immediately and it works much the same way as the interpreter at Simply Scala. To run REPL, type the following into the terminal:
This should result in an output similar to
felix@vaio:~$ scala Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_24). Type in expressions to have them evaluated. Type :help for more information. scala>
Let’s try a few things in REPL:
scala> println("Hello, World!") Hello, World! scala> println("Hello " * 3) Hello Hello Hello scala> println("Hello, World!" reverse) !dlroW ,olleH
Printing things to the screen is basic stuff in any programming language, but when you are using REPL the results will usually be printed to the screen without explicitly using println():
scala> "Hello, World!" res0: java.lang.String = Hello, World! scala> "Hello " * 3 res1: String = Hello Hello Hello scala> "Hello, World!" reverse res2: String = !dlroW ,olleH
Notice, that not only do you see the contents of the results when you do a calculation, you also see the type of the result. Furthermore, you can refer to the result by using the names res0, res1, res2 and so on.
Your first FUNction
First, let’s try a few things that could be worth automating:
scala> 1+2+3+4+5 res0: Int = 15 scala> var number = 1 number: Int = 1 scala> for(i <- 2 to 5) number = number + i scala> number res:2 Int = 15
First we add together a few numbers, just like a calculator. Next up, we create a variable and initialize it’s content to 1. Now we simply sum up in the same way as before, but this time we use a for-loop. The syntax is very straight forward. Set a variable i to the values from 2 to 5 (to is inclusive) and each time set the value of number to it’s current value plus i. Most people can see that this is in fact the mathematical concept summation. Let’s generalize it! To do this we need a function:
scala> def sum(n:Int) = 1 to n reduce (_+_) sum: (n: Int)Int scala> sum(5) res2: Int = 15
Let’s see what happened here. def means define a function and we simply picked the identifier sum because it describes the purpose of the function. Now, functions can take arguments just like functions you already know from math like sine, cosine, logarithm and so on. In this case we take a single argument, denote it n and tell Scala that it needs to be an integer. Using the equals sign, we tell Scala that this function has a value, in other words it returns something.
1 to n
seems familiar. We used this in the for loop to denote a range. This is the same thing, only we use the argument n to be the end of the range. reduceRight is a function that can be used on a range (or most sequences in Scala). It works by taking the leftmost value and combining it with the next value, then taking the result of that combination and combining it with the next value again until finally you are left with a single value. The final part
is simply a function to use when combining the values. In this case it just adds together two values.
That’s it. Your very first scala function. Try to apply it with different values of n. Also, try to use multiplication, division and subtraction instead.
In the next part I will show you the other way to work with Scala, namely compiling scala programs from text-files. I hope you enjoyed the tutorial so far. And don’t worry, I will pick up the pace as we get to the next parts :)