/** * 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. */ scala> def signum(num:Int) = if (num > 0) 1 else if (num < 0) -1 else 0 signum: (num: Int)Int scala> signum(10) res4: Int = 1 scala> signum(0) res5: Int = 0 scala> signum(-123) res6: Int = -1 /** * 2. What is the value of an empty block expression {}? What is its type? */ // Value is 'no value' and type is Unit scala> val empty = {} empty: Unit = () scala> empty /** * 3. Come up with one situation where the assignment x = y = 1 is valid in Scala. * (Hint: Pick a suitable type for x.) */ scala> var x: Any = _ x: Any = null scala> var y: Int = _ y: Int = 0 scala> x = y = 1 x: Any = () scala> x res0: Any = () scala> y res1: Int = 1 /** * 4. Write a Scala equivalent for the Java loop * for (int i = 10; i >= 0; i--) System.out.println(i); */ // Range.by: Create a new range with the start and end values of this range and a new step. scala> for(i<- 10 to 0 by -1) println(i) 10 9 8 7 6 5 4 3 2 1 0 /** * 5. Write a procedure countdown(n: Int) that prints the numbers from n to 0. */ scala> def countdown(n:Int) = for(i <- n to 0 by -1) println(i) countdown: (n: Int)Unit scala> countdown(10) 10 9 8 7 6 5 4 3 2 1 0 /** * 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. */ // IndexedSeq.product: Multiplies up the elements of this collection. // returns // the product of all elements in this sequence of numbers of type Int. // Instead of Int, any other type T with an implicit Numeric[T] implementation can be used as element type of the sequence and as result type of product. // Examples of such types are: Long, Float, Double, BigInt. scala> (for(i <- "Hello") yield i.toLong).product res19: Long = 9415087488 /** * 7. Solve the preceding exercise without writing a loop. (Hint: Look at the StringOps Scaladoc.) */ scala> "Hello".map(c => c.toLong).product res22: Long = 9415087488 /** * 8. Write a function product(s : String) that computes the product, as described in the preceding exercises. */ scala> def product(s:String) = s.map(c => c.toLong).product product: (s: String)Long scala> product("Hello") res23: Long = 9415087488 /** * 9. Make the function of the preceding exercise a recursive function. */ scala> def product(s:String, p:Long):Long = s.isEmpty match { | case false => product(s.tail, p * s.head.toLong) | case true => p | } scala> def prod(s:String):Long = s.isEmpty match { | case false => product(s, 1) | case true => 0 | } prod: (s: String)Long scala> prod("") res5: Long = 0 scala> prod("Hello") res6: Long = 9415087488 /** * 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 = xn / 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. */ scala> def pow(x:Int, n:Int) = n match { | case n if n > 0 && (n%2 == 0) => scala.math.pow(x, n/2) | case n if n > 0 && (n%2 == 1) => x * scala.math.pow(x, n-1) | case 0 => 1 | case n if n < 0 => 1/scala.math.pow(x, -n) | } pow: (x: Int, n: Int)Double scala> pow(2, 4) res0: Double = 4.0 scala> pow(2, 5) res1: Double = 32.0 scala> pow(2, 0) res2: Double = 1.0 scala> pow(2, -1) res3: Double = 0.5