Skip to content

Latest commit

 

History

History
98 lines (59 loc) · 4.12 KB

ch1-GettingStarted.asciidoc

File metadata and controls

98 lines (59 loc) · 4.12 KB

Exercises

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?

Answer

An alternate method to using println() is the print() command, which prints the given string without appending a newline.

scala> print("Please remain on the line")
Please remain on the line

When you’re working in the REPL (Read-Eval-Print-Loop), you can also just enter your string on its own line. The "Print" functionality in the REPL will just print the given value.

scala> "Hello, REPL"
res0: String = Hello, REPL

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.

Answer

The standard math operators and precedences used in other mainstream programming languages also work in Scala. Thus you can be assured that multiplication and division operators take precedence over addition and subtraction operators.

scala> 22.5 * 9 / 5 + 32
res0: Double = 72.5

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.

Answer

Using parentheses around the subtraction, which needs to occur before the multiplication and division, will give it highest precedence.

scala> 22.5 * 9 / 5 + 32
res0: Double = 72.5

scala> (res0 - 32) * 5 / 9
res1: Double = 22.5

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.

Answer

Here’s my "Hello.scala" file:

println("Greetings from 'Hello.scala'")

To load a file from the same directory, type ":load" and the file name, without quotes.

scala> :load Hello.scala
Loading Hello.scala...
Greetings from 'Hello.scala'

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.

Note The exercise is incorrect - "raw" mode should not be used here. Please use ":paste" to paste your println command into the REPL. The "raw" mode in the REPL expects a Scala class or equivalent.

Answer

Here is the contents of "Hello.scala", pasted from the clipboard into the REPL’s paste mode. As noted, you’ll need to press "<control>-d" to exit out of the paste mode and trigger the evaluation of the contents. Make sure to use ":paste" here, and not ":paste -raw" as the exercise incorrectly states.

scala> :paste
// Entering paste mode (ctrl-D to finish)

println("Greetings from 'Hello.scala'")

// Exiting paste mode, now interpreting.

Greetings from 'Hello.scala'