forked from Y1ran/Scala-and-Spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tail_recur.sc
37 lines (30 loc) · 1.16 KB
/
Tail_recur.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
object Tail_recur {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//@scala.annotation.tailrec
def fact(n: Int, m: Int) : Int ={
if ( n <= 0) m
else fact( n-1 , m * n)
} //> fact: (n: Int, m: Int)Int
fact(10,1) //> res0: Int = 3628800
def sum(f: Int => Int)(a:Int)(b:Int): Int={
@scala.annotation.tailrec
def loop(n:Int, acc:Int): Int={
if( n > b ) {
println(s"n=${n}, acc = ${acc}")
acc
}
else {
println(s"n=${n}, acc = ${acc}")
loop(n + 1, acc + f(n))
}
}
loop(a,0)
} //> sum: (f: Int => Int)(a: Int)(b: Int)Int
sum(x => x)(1)(5) //> n=1, acc = 0
//| n=2, acc = 1
//| n=3, acc = 3
//| n=4, acc = 6
//| n=5, acc = 10
//| n=6, acc = 15
//| res1: Int = 15
}