Fahad/Scala/WorkSheet.sc
changeset 30 197babb05539
equal deleted inserted replaced
29:2345ba5b4264 30:197babb05539
       
     1 package greeter
       
     2 
       
     3 object WorkSheet {
       
     4   println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
       
     5 
       
     6   val x = 5                                       //> x  : Int = 5
       
     7   def increase(i: Int) = i + 1                    //> increase: (i: Int)Int
       
     8   increase(x)                                     //> res0: Int = 6
       
     9 
       
    10   // Expressions And Simple Functions
       
    11 
       
    12   87 + 145                                        //> res1: Int(232) = 232
       
    13   5 + 2 * 3                                       //> res2: Int = 11
       
    14 
       
    15   "Hello" + " World!"                             //> res3: String("Hello World!") = Hello World!
       
    16 
       
    17   def scale = 5                                   //> scale: => Int
       
    18   7 * scale                                       //> res4: Int = 35
       
    19 
       
    20   def pi = 3.141592653589793                      //> pi: => Double
       
    21   def radius = 10                                 //> radius: => Int
       
    22   2 * pi * radius                                 //> res5: Double = 62.83185307179586
       
    23 
       
    24   // PARAMETERS
       
    25 
       
    26   def square(x: Double) = x * x                   //> square: (x: Double)Double
       
    27   square(2)                                       //> res6: Double = 4.0
       
    28   square(square(4))                               //> res7: Double = 256.0
       
    29 
       
    30   def sumOfSquares(x: Double, y: Double) = square(x) + square(y)
       
    31                                                   //> sumOfSquares: (x: Double, y: Double)Double
       
    32   sumOfSquares(3, 2 + 2)                          //> res8: Double = 25.0
       
    33 
       
    34   def loop: Int = loop                            //> loop: => Int
       
    35   def first(x: Int, y: Int) = x                   //> first: (x: Int, y: Int)Int
       
    36   def constOne(x: Int, y: => Int) = 1             //> constOne: (x: Int, y: => Int)Int
       
    37   constOne(1, loop)                               //> res9: Int = 1
       
    38 
       
    39   // 4.3 CONDITIONAL EXPRESSIONS
       
    40 
       
    41   def abs(x: Double) = if (x >= 5) x + 1 else x - 1
       
    42                                                   //> abs: (x: Double)Double
       
    43   abs(4)                                          //> res10: Double = 3.0
       
    44 
       
    45   // 4.4: Square Roots by Newton's Method
       
    46 
       
    47   def sqrtIter(guess: Double, x: Double): Double =
       
    48     if (isGoodEnough(guess, x)) guess
       
    49     else sqrtIter(improve(guess, x), x)           //> sqrtIter: (guess: Double, x: Double)Double
       
    50 
       
    51   def improve(guess: Double, x: Double) =
       
    52     (guess + x / guess) / 2                       //> improve: (guess: Double, x: Double)Double
       
    53 
       
    54   def isGoodEnough(guess: Double, x: Double) =
       
    55     abs(square(guess) - x) < 0.001                //> isGoodEnough: (guess: Double, x: Double)Boolean
       
    56 
       
    57   //def sqrt(x: Double) = sqrtIter(1.0, x)
       
    58   //sqrt(25)
       
    59 
       
    60   // 4.5: Nested Functions
       
    61 
       
    62   def sqrt(x: Double) = {
       
    63     def sqrtIter(guess: Double): Double =
       
    64       if (isGoodEnough(guess)) guess
       
    65       else sqrtIter(improve(guess))
       
    66     def improve(guess: Double) =
       
    67       (guess + x / guess) / 2
       
    68     def isGoodEnough(guess: Double) =
       
    69       abs(square(guess) - x) < 0.001
       
    70     sqrtIter(1.0)
       
    71   }                                               //> sqrt: (x: Double)Double
       
    72   sqrt(25)                                        //> res11: Double = 1.0
       
    73 
       
    74   // 4.6: Tail Recursion
       
    75 
       
    76   def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
       
    77                                                   //> gcd: (a: Int, b: Int)Int
       
    78 
       
    79   gcd(21, 36)                                     //> res12: Int = 3
       
    80 
       
    81 }