This post summarises notes and/or exercise solutions of Chapter 2 Working With Data: Literals, Values, Variables, and Types of Learning Scala by Jason Swartz. More complete solutions can be found HERE. Scala code is originally executed in a Eclipse Scala worksheet.
The Unit type is unlike the other core types here (numeric and non-numeric) in that instead of denoting a type of data it denotes the lack of data.
& and &&? - | (or ||) don’t evaluate the second argument if the first is sufficient
Scala doesn’t support automatic conversions to booleans eg non-null strings is not true, 0 is not false
1. Write a new centigrade-to-fahrenheit conversion (using the formula (x * 9/5) + 32), saving each step of the conversion into separate values. What do you expect the type of each value will be?
2. Modify the centigrade-to-fahrenheit formula to return an integer instead of a floating-point number.
3. Using the input value 2.7255, generate the string You owe $2.73 dollars. Is this doable with string interpolation?
4. Is there a simpler way to write the following?
5. Convert the number 128 to a Char, a String, a Double, and then back to an Int. Do you expect the original amount to be retained? Do you need any special conversion functions for this?
6. Using the input string Frank,123 Main,925-555-1943,95122 and regular expression matching, retrieve the telephone number. Can you convert each part of the telephone number to its own integer value? How would you store this in a tuple?