"Quick Learning Scala" exercises 2nd Chapter control Structure and function

Source: Internet
Author: User
Tags new set

1 If a number is positive, its signum is 1, if it is negative, then Signum is-1; if it is 0, Signum is 0

   def f (x:int): Int = {
      //If f is not recursive, it can be omitted: int,= is automatically type inferred
      //= used to receive the return value, does not need to return a value when not =
      //If no =,println () will print ()
      if (x > 0) 1
      else if (x = = 0) 0
      else-1
    }
    println (f (1))
    println (f (-1))

What is the value of a 21 empty block expression. What is the type of it?
The value of the expression is (), the type is unit
Unit equals void in Java with only one value ()
In Scala, the value of the last expression in the {} is the value of the block

scala> val x = {}
x:unit = ()

3 indicates the circumstances under which the assignment statement X=y=1 is legal
is legal in the case of the type of X as unit

scala> var y=1
y:int = 1

scala> val x = y = 2
x:unit = ()

scala> val x = (y = 2)
X:un it = ()

4 Write a Scala version for the following Java loops
for (int i=10;i>=0;i–) System.out.println (i)

for (I <-(1 to ten). Reverse) println (i)
//10 To/until 1 Not responding, To/until can only be used to write a positive order, and then reverse

5 Writing a procedure countdown (n:int), printing numbers from N to 0
Compared to a function, the procedure is simply that there is no return value and does not need to be used =

EF F (n:int) {for (I <-1 to N) println (i)}
    f (5)

6 Write A For loop that calculates the product of Unicode code for all letters in a string
For example, the characters of all letters in "Hello" become 9415087488L
Unicode still uses ASCLL codes for encoding letters.

   def f (s:string) {
      var sum:long = 1 for
      (i <-s) sum *= i
      println (sum)
    }
    F ("Hello")

Using the abstract def* (X:char): Int method in Char

7 is also the problem of solving 6, but not enough to cycle (hint, stringops)

    def f (s:string) {
      var sum:long = 1
      sum = s.map {_.tolong}.product
            //defmap[b] (f: (A) ⇒b): String[b], by function F map each element in string to a new set 
      /Def product:a, multiplying each element in the collection by
      println (sum)
    }

    F ("Hello")

8 Writing function product (s:string), solving the previous exercise
Add a previous procedure = can, no longer repeat

9 Change the function of the previous exercise to a recursive function

    var Sum:long = 1
    def f (s:string): String = {
      if (s.length () >= 1) {
        sum *= s (0)
        //intercept first character var per call
        S2 = s.drop (1)
        f (S2)//recursion, the last line may not be a string
      } else ""//recursive need to specify the return value type 
    }
    F ("Hello")
    println (sum)

10 The Write function computes the n-square of x, where n is an integer, using the following recursive definition:
Feel the problem of translation, ask X's n-th square

    def f (X:int, n:int): Double = {
      if (n >= 1) {
        //= n is not an even number, if it is even, it becomes two multiplied if
        (n 2 = = 0) f (x, N/2) * f (x, N/2)
        If it is odd, the n minus 1, becomes even, continues to be determined until n=1
        else x * F (x, n-1)
      } else if (n = = 0) 1
      //If n is a negative number and becomes a positive number to solve
      else -N)
    }
    println (f (2))
    println (f (10,-2))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.