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