Exercises

Let's check our understanding of the core concepts in this chapter with some exercises.

Exercise: Arithmetic

Write an expression using integers, addition, and subtraction that evaluates to 42.

This exercise is just about getting used to writing Scala code. There are many possible solutions.

Here's how I chose to do it.

1 + 43 - 2
// res0: Int = 42

Exercise: Precedence

In mathematics we learned that some operators take precedence over others. For example, in the expression 1 + 2 * 3 we should do the multiplication before the addition. Do the same rules hold in Scala? Use the worksheet to find out.

A bit of exploration should convince you that yes, Scala does maintain the standard precedence rules. The example below demonstrates this.

1 + 2 * 3
// res1: Int = 7
1 + (2 * 3)
// res2: Int = 7
(1 + 2) * 3
// res3: Int = 9

Exercise: Types and Values

Below we've written a number of expressions. For each expression, try to answer the questions:

  1. It will compile?
  2. If it compiles, what is it's type?
  3. If it compiles can it be evaluated, or will it fail at run-time?

Then try to check your guesses using the worksheet. If you guessed wrong, try to think about what part of your understanding is incorrect.

1 + 2
1 ? 2
4 / 2
1 / 2
1 / 0
1 + 2
// res8: Int = 3

Easy example as we've seen this one before. This expression has type Int and evaluates to 3.

1 ? 2
// error:
// value ? is not a member of Int
// 1 ? 2
// ^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 43 - 2
// ^^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 2 * 3
// ^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + (2 * 3)
// ^^^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// (1 + 2) * 3
// ^^^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 4 / 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 / 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 2
// ^^^^^

This is not a valid expression and does not compile. As such it doesn't have a type.

4 / 2
// res10: Int = 2

This expression has type Int and evaluates to 2.

1 / 2
// res11: Int = 0

This expression has type Int and evaluates to 0. People coming from dynamically typed languages will sometimes guess that this evaluates to 0.5 and hence the type would be a floating point number. This would require we pass information back from the run-time to compile-time, which is not possible.

1 / 0
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 43 - 2
// ^^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 2 * 3
// ^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + (2 * 3)
// ^^^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// (1 + 2) * 3
// ^^^^^^^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 4 / 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 / 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 + 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 4 / 2
// ^^^^^
// error:
// A pure expression does nothing in statement position; you may be omitting necessary parentheses
// 1 / 2
// ^^^^^

This expression has type Int but fails at run-time. This exercise is emphasizing that the type is a property of the expression, not the value, and types are not determined by run time behaviour.