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:
- Will it compile?
- If it compiles, what is its type?
- 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
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
This is not a valid expression and does not compile. As such it doesn't have a type.
4 / 2
// res8: Int = 2
This expression has type Int
and evaluates to 2
.
1 / 2
// res9: 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
// java.lang.ArithmeticException: / by zero
// at repl.MdocSession$MdocApp.$init$$$anonfun$1(05-exercises.md:73)
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.