This post summarises notes and/or exercise solutions of Chapter 1 Getting Started With The Scalable Language of Learning Scala by Jason Swartz. More complete solutions can be found HERE. Scala code is originally executed in a Eclipse Scala worksheet.


1. Although println() is a good way to print a string, can you find a way to print a string without println? Also, what kind of numbers, strings and other data does the REPL support?
print("a string")
2. In the Scala REPL, convert the temperature value of 22.5 Centigrade to Fahrenheit. The conversion formula is cToF(x) = (x * 9/5) + 32.
var celTemp = 22.5
var fahTemp = (celTemp * 9/5) + 32
3. Take the result from exercise 2, half it, and convert it back to Centigrade. You can use the generated constant variable (e.g. res0) instead of copying and pasting the value yourself.
celTemp = (fahTemp/2 - 32) * 5/9
4. The REPL can load and interpret Scala code from an external file with the :load <file> command. Create a new file named Hello.scala and add a command that will print a greeting, then execute it from the REPL.
// Not working in scala worksheet and tested in REPL
:load Hello.scala

// note: some ways to check current path
5. Another way to load external Scala code is to paste it into the repl in raw mode, where the code is compiled as if it were actually in a proper source file. To do this, type :paste -raw, hit return, and then paste the contents of your source file from exercise 4. After exiting paste mode you should see the greeting.
// Not working in scala worksheet and tested in REPL
// Entering paste mode (ctrl-D to finish)
println("Hello world")

// Exiting paste mode, now interpreting.
Hello world