Scala for the Impatient -- 2nd Edition


Chapter 2. Control Structures and Functions


In [1]:
println(s"""Details of exec env ==>
    |    ${util.Properties.versionMsg}
    |    ${util.Properties.javaVmName} ${util.Properties.javaVersion} ${util.Properties.javaVmVersion}"""
.stripMargin)


Details of exec env ==>
    Scala library version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
    Java HotSpot(TM) 64-Bit Server VM 1.8.0_131 25.131-b11

Qn#1. The signum of a number is 1 if the number is positive, –1 if it is negative, and 0 if it is zero. Write a function that computes this value.


In [2]:
def signum(number: Int) = {
    if (number > 0) 1
    else if (number < 0) -1
    else 0;
}

println(signum(-50), signum(0), signum(50))


(-1,0,1)

Qn#4. Write a Scala equivalent for the Java loop for (int i = 10; i >= 0; i--) System.out.println(i);


In [3]:
for (n <- Range(10, -1, -1)) println(n)


10
9
8
7
6
5
4
3
2
1
0

Qn#5. Write a procedure countdown(n: Int) that prints the numbers from n to 0.


In [4]:
def countdown(n: Int) {
    for( i <- Range(n, -1, -1) )
        println(i)
}

countdown(5);


5
4
3
2
1
0

Qn#6. Write a for loop for computing the product of the Unicode codes of all letters in a string. For example, the product of the characters in "Hello" is 9415087488L.


In [5]:
var prod = 1l

for (c <- "Hello") {
    prod *= c
    println(c, prod)
}


(H,72)
(e,7272)
(l,785376)
(l,84820608)
(o,9415087488)

Qn#7. Solve the preceding exercise without writing a loop. (Hint: Look at the StringOps Scaladoc.)


In [6]:
"Hello".foldLeft(1L)((a, b) => a * b)


Out[6]:
9415087488

Qn#8. Write a function product(s : String) that computes the product, as described in the preceding exercises.


In [7]:
def product(s: String) = {
    s.foldLeft(1L)((a, b) => a * b)
}

println(product("Hello"))


9415087488

Qn#9. Make the function of the preceding exercise a recursive function.


In [8]:
def productRecursive(s: String):Long = {
    if(s.length == 0) 1
    else s(0) * productRecursive(s drop 1)
}

productRecursive("Hello")


Out[8]:
9415087488

Qn#10. Write a function that computes xn, where n is an integer. Use the following recursive definition:

  • xn = y2 if n is even and positive, where y = x n / 2.
  • xn = x * xn – 1 if n is odd and positive.
  • x0 = 1.
  • xn = 1 / x–n if n is negative.

Don’t use a return statement.


In [9]:
def powerOfXToN(x: BigInt, n: Int) : BigInt = {
    if(n > 0 && n % 2 == 0) powerOfXToN(x, n / 2) * powerOfXToN(x, n / 2)
    else if(n > 0) x * powerOfXToN(x, n - 1)
    else if(n == 0) 1
    else 1 / powerOfXToN(x, -n)
}

powerOfXToN(2, 100)


Out[9]:
1267650600228229401496703205376

Qn#11. Define a string interpolator date so that you can define a java.time.LocalDate as date"$year-$month-$day". You need to define an implicit class with a date method, like this:

implicit class DateInterpolator(val sc: StringContext) extends AnyVal { def date(args: Any*): LocalDate = . . . }

args(i) is the value of the ith expression. Convert each to a string and then to an integer, and pass them to the LocalDate.of method. If you already know some Scala, add error handling. Throw an exception if there aren’t three arguments, or if they aren’t integers, or if they aren’t separated by dashes. (You get the strings in between the expressions as sc.parts.)


In [10]:
println("TBC")


TBC