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