Learning Scala in 5 parts: Part 1


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!

You’ll need…

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

Let’s try a few things in REPL:

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():

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:

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:

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.

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 🙂

Part 2


Leave a Reply

Your email address will not be published. Required fields are marked *