My solutions for "Scala for the Impatient" exercises
The [AL][1-3] refer to Martin Odersky's Scala levels.
Control Structures and Functions (A1)
Files and Regular Expressions (A1))
1. In the Scala REPL, type 3. followed by the Tab key. What methods can be applied?
3. Are the res variables val or var ?
5. What does 10 max 2 mean? In which class is the max method defined?
6. Using BigInt , compute 2 1024 .
9. How do you get the first character of a string in Scala? The last character?
Control Structures and Functions (A1)
2. What is the value of an empty block expression {} ? What is its type?
4. Write a Scala equivalent for the Java loop for (int i = 10; i >= 0; i--) System.out.println(i);
5. Write a procedure countdown(n: Int) that prints the numbers from n to 0.
7. Solve the preceding exercise without writing a loop. (Hint: Look at the StringOps Scaladoc.)
9. Make the function of the preceding exercise a recursive function.
5. How do you compute the average of an Array[Double] ?
3. Repeat the preceding exercise with an immutable map.
4. Repeat the preceding exercise with a sorted map, so that the words are printed in sorted order.
5. Repeat the preceding exercise with a java.util.TreeMap that you adapt to the Scala API.
2. Write a class BankAccount with methods deposit and withdraw , and a read-only property balance .
7. Implement a function that checks whether a card suit value from the preceding exercise is red.
2. Write a puzzler that baffles your Scala friends, using a package com that isn’t at the top level.
5. What is the meaning of private[com] def giveRaise(rate: Double) ? Is it useful?
7. In the preceding exercise, move all imports into the innermost scope possible.
8. What is the effect of import java._ import javax._ Is this a good idea?
10. Apart from StringBuilder , what other members of java.lang does the scala package override?
Files and Regular Expressions (A1))
According to the precedence rules, how are 3 + 4 -> 5 and 3 -> 4 + 5 evaluated?
Implement the Fraction class with operations + -
Provide a class ASCIIArt whose objects contain figures such as /_/\ ( ' ' ) ( - ) | | | precedence.