Home‎ > ‎

Why Scala for CS1 and CS2?

There are many choices one can make for a language to teach CS1 and CS2 in. The language that is currently most broadly used is Java with C and C++ also having a strong representation, in part due to holding the top position in the past. In recent years, Python has also been making waves in CS pedagogy circles. The arguments for Python focus largely on the simplicity of the language and the fact that it has both a REPL and a scripting environment, which provide strong support for programming in the small. This is very important for beginner programmers as the keyword soup that is required to write just "Hello World" in some languages causes problems on the first day.

These factors make Python great for CS1, but not everyone would consider it ideal for CS2 because of the dynamic typing and the way the fact that the language was really designed to be a scripting language, not to scale to larger applications. Scala has the same advantages of Python for programming in the small, but adds on static type checking in a robust type system as well as a solid application programming model intended to scale to large projects.

Most educators have been working with Java for a decade or more. That implies that we have a significant knowledge base built up on that platform. In addition, Java libraries for all types of different tasks are readily available on the web. Another strength of Scala is that the primary implementation is JVM and there is a seamless syntax for calling Java code. In fact, it is easy to mix Java and Scala code together in the same project. This means educators can change to a more modern language without throwing out all their base knowledge. It also means that advanced students can easily find powerful libraries for whatever they want to do, even if it isn't written in Scala.

Scala also has very strong support for parallel work. Teaching students about parallelism is becoming more and more essential. It is increasingly important to have tools to help demonstrate the essential skills. Scala allows full access to the Java thread libraries, but also includes an actors library as well as parallel collections for easy data parallelism. Using the latter, you can introduce parallelism as soon as you have covered for loops using code like the following.

def factorial(n:BigInt):BigInt = if(n<2) 1 else n*factorial(n-1)

for(i <- 30 to 15 by -1) println(factorial(i))
for(i <- 30 to 15 by -1 par) println(factorial(i))

This does a simple demonstration of how order can vary in parallel code. You can also show the challenges of race-conditions with code like the following.

var cnt = 0
for(i <- 1 to 1000000000 par) cnt += 1

You can decide when you want to introduce harder concepts, but Scala gives you the tools to make it easy.

Blog Posts
I have written some blog posts about why we chose Scala at Trinity and many more details on how it is beneficial for CS1 that you can find at the links below.
Why Scala for CS1 and CS2?
Benefits of Scala in CS1
More Benefits of Scala in CS1