# HG changeset patch # User fahadausaf # Date 1414254536 -3600 # Node ID 197babb055393faedcee2a7c5593c2cc02bbdf33 # Parent 2345ba5b42648e2b80729a5f18fe6bd399306508 scala by example code diff -r 2345ba5b4264 -r 197babb05539 Fahad/Scala/Chapter5.sc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Fahad/Scala/Chapter5.sc Sat Oct 25 17:28:56 2014 +0100 @@ -0,0 +1,55 @@ +package greeter + +object Chapter5 { + println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet + + ///////////////////////////////////////////////////////////////////////////// + // CHAPTER 5: FIRST-CLASS FUNCTIONS + + /* + // Write a function to sumall integers between two given numbers a and b + def sumInts(a: Int, b: Int): Int = + if (a > b) 0 else a + sumInts(a + 1, b) + + sumInts(1, 5) + + //Write a function to sum the squares of all integers between two given numbers a and b + def square(x: Int): Int = x * x + def sumSquares(a: Int, b: Int): Int = + if (a > b) 0 else square(a) + sumSquares(a + 1, b) + + square(5) + + //Write a function to sum the powers 2n of all integers n between two given numbers a and b: + def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1) + def sumPowersOfTwo(a: Int, b: Int): Int = + if (a > b) 0 else powerOfTwo(a) + sumPowersOfTwo(a + 1, b) + + sumPowersOfTwo(2, 4) + */ + + def sum(f: Int => Int, a: Int, b: Int): Int = + if (a > b) 0 else f(a) + sum(f, a + 1, b) //> sum: (f: Int => Int, a: Int, b: Int)Int + + //def sumInts(a: Int, b: Int): Int = sum(id, a, b) + //def sumInts(a: Int, b: Int): Int = sum((x: Int) => x, a, b) + def sumInts(a: Int, b: Int): Int = sum(x => x, a, b) + //> sumInts: (a: Int, b: Int)Int + //def sumSquares(a: Int, b: Int): Int = sum(square, a, b) + //def sumSquares(a: Int, b: Int): Int = sum((x: Int) => x * x, a, b) + def sumSquares(a: Int, b: Int): Int = sum(x => x * x, a, b) + //> sumSquares: (a: Int, b: Int)Int + def sumPowersOfTwo(a: Int, b: Int): Int = sum(powerOfTwo, a, b) + //> sumPowersOfTwo: (a: Int, b: Int)Int + + def id(x: Int): Int = x //> id: (x: Int)Int + def square(x: Int): Int = x * x //> square: (x: Int)Int + def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1) + //> powerOfTwo: (x: Int)Int + + // 5.2: Currying + + + + +} \ No newline at end of file diff -r 2345ba5b4264 -r 197babb05539 Fahad/Scala/WorkSheet.sc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Fahad/Scala/WorkSheet.sc Sat Oct 25 17:28:56 2014 +0100 @@ -0,0 +1,81 @@ +package greeter + +object WorkSheet { + println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet + + val x = 5 //> x : Int = 5 + def increase(i: Int) = i + 1 //> increase: (i: Int)Int + increase(x) //> res0: Int = 6 + + // Expressions And Simple Functions + + 87 + 145 //> res1: Int(232) = 232 + 5 + 2 * 3 //> res2: Int = 11 + + "Hello" + " World!" //> res3: String("Hello World!") = Hello World! + + def scale = 5 //> scale: => Int + 7 * scale //> res4: Int = 35 + + def pi = 3.141592653589793 //> pi: => Double + def radius = 10 //> radius: => Int + 2 * pi * radius //> res5: Double = 62.83185307179586 + + // PARAMETERS + + def square(x: Double) = x * x //> square: (x: Double)Double + square(2) //> res6: Double = 4.0 + square(square(4)) //> res7: Double = 256.0 + + def sumOfSquares(x: Double, y: Double) = square(x) + square(y) + //> sumOfSquares: (x: Double, y: Double)Double + sumOfSquares(3, 2 + 2) //> res8: Double = 25.0 + + def loop: Int = loop //> loop: => Int + def first(x: Int, y: Int) = x //> first: (x: Int, y: Int)Int + def constOne(x: Int, y: => Int) = 1 //> constOne: (x: Int, y: => Int)Int + constOne(1, loop) //> res9: Int = 1 + + // 4.3 CONDITIONAL EXPRESSIONS + + def abs(x: Double) = if (x >= 5) x + 1 else x - 1 + //> abs: (x: Double)Double + abs(4) //> res10: Double = 3.0 + + // 4.4: Square Roots by Newton's Method + + def sqrtIter(guess: Double, x: Double): Double = + if (isGoodEnough(guess, x)) guess + else sqrtIter(improve(guess, x), x) //> sqrtIter: (guess: Double, x: Double)Double + + def improve(guess: Double, x: Double) = + (guess + x / guess) / 2 //> improve: (guess: Double, x: Double)Double + + def isGoodEnough(guess: Double, x: Double) = + abs(square(guess) - x) < 0.001 //> isGoodEnough: (guess: Double, x: Double)Boolean + + //def sqrt(x: Double) = sqrtIter(1.0, x) + //sqrt(25) + + // 4.5: Nested Functions + + def sqrt(x: Double) = { + def sqrtIter(guess: Double): Double = + if (isGoodEnough(guess)) guess + else sqrtIter(improve(guess)) + def improve(guess: Double) = + (guess + x / guess) / 2 + def isGoodEnough(guess: Double) = + abs(square(guess) - x) < 0.001 + sqrtIter(1.0) + } //> sqrt: (x: Double)Double + sqrt(25) //> res11: Double = 1.0 + + // 4.6: Tail Recursion + + def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) + //> gcd: (a: Int, b: Int)Int + + gcd(21, 36) //> res12: Int = 3 + +} \ No newline at end of file