1 |
|
2 package greeter |
|
3 |
|
4 object Chapter5 { |
|
5 println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet |
|
6 |
|
7 ///////////////////////////////////////////////////////////////////////////// |
|
8 // CHAPTER 5: FIRST-CLASS FUNCTIONS |
|
9 |
|
10 /* |
|
11 // Write a function to sumall integers between two given numbers a and b |
|
12 def sumInts(a: Int, b: Int): Int = |
|
13 if (a > b) 0 else a + sumInts(a + 1, b) |
|
14 |
|
15 sumInts(1, 5) |
|
16 |
|
17 //Write a function to sum the squares of all integers between two given numbers a and b |
|
18 def square(x: Int): Int = x * x |
|
19 def sumSquares(a: Int, b: Int): Int = |
|
20 if (a > b) 0 else square(a) + sumSquares(a + 1, b) |
|
21 |
|
22 square(5) |
|
23 |
|
24 //Write a function to sum the powers 2n of all integers n between two given numbers a and b: |
|
25 def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1) |
|
26 def sumPowersOfTwo(a: Int, b: Int): Int = |
|
27 if (a > b) 0 else powerOfTwo(a) + sumPowersOfTwo(a + 1, b) |
|
28 |
|
29 sumPowersOfTwo(2, 4) |
|
30 */ |
|
31 |
|
32 /* |
|
33 def sum(f: Int => Int, a: Int, b: Int): Int = |
|
34 if (a > b) 0 else f(a) + sum(f, a + 1, b) |
|
35 |
|
36 //def sumInts(a: Int, b: Int): Int = sum(id, a, b) |
|
37 //def sumInts(a: Int, b: Int): Int = sum((x: Int) => x, a, b) |
|
38 def sumInts(a: Int, b: Int): Int = sum(x => x, a, b) |
|
39 //def sumSquares(a: Int, b: Int): Int = sum(square, a, b) |
|
40 //def sumSquares(a: Int, b: Int): Int = sum((x: Int) => x * x, a, b) |
|
41 def sumSquares(a: Int, b: Int): Int = sum(x => x * x, a, b) |
|
42 def sumPowersOfTwo(a: Int, b: Int): Int = sum(powerOfTwo, a, b) |
|
43 |
|
44 def id(x: Int): Int = x |
|
45 def square(x: Int): Int = x * x |
|
46 */ |
|
47 def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1) |
|
48 //> powerOfTwo: (x: Int)Int |
|
49 |
|
50 |
|
51 // 5.2: Currying |
|
52 |
|
53 def sum(f: Int => Int): (Int, Int) => Int = { |
|
54 def sumF(a: Int, b: Int): Int = |
|
55 if (a > b) 0 else f(a) + sumF(a + 1, b) |
|
56 sumF |
|
57 } //> sum: (f: Int => Int)(Int, Int) => Int |
|
58 |
|
59 def sumInts = sum(x => x) //> sumInts: => (Int, Int) => Int |
|
60 def sumSquares = sum(x => x * x) //> sumSquares: => (Int, Int) => Int |
|
61 def sumPowersOfTwo = sum(powerOfTwo) //> sumPowersOfTwo: => (Int, Int) => Int |
|
62 |
|
63 sumSquares(1,10) + sumPowersOfTwo(10,20) //> res0: Int = 2096513 |
|
64 sum(x => x * x)(2,4) //> res1: Int = 29 |
|
65 |
|
66 // 5.3: Finding Fixed Points of Functions |
|
67 |
|
68 val tolerance = 0.001 //> tolerance : Double = 0.001 |
|
69 def isCloseEnough(x: Double, y: Double) = Math.abs((x-y) / x) < tolerance |
|
70 //> isCloseEnough: (x: Double, y: Double)Boolean |
|
71 def fixedPoint(f: Double => Double)(firstGuess: Double) = { |
|
72 def iterate(guess: Double): Double = { |
|
73 val next = f(guess) |
|
74 if (isCloseEnough(guess, next)) next |
|
75 else iterate(next) |
|
76 } |
|
77 iterate(firstGuess) |
|
78 } //> fixedPoint: (f: Double => Double)(firstGuess: Double)Double |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 } |
|