package greeter
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
object tests {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
// val z = 1 to 1000
// val L1 = List(z)
val L2 = List(1, 2, 3, 4) //> L2 : List[Int] = List(1, 2, 3, 4)
def listTestA() ={
var list1:List[Int] = Nil
for(i <- 0 to 30000)
list1 = list1 ::: List(i)
list1
} //> listTestA: ()List[Int]
listTestA() //> res0: List[Int] = List(0, 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)
var L3 = listTestA() //> L3 : List[Int] = List(0, 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)
def myListLength(x: List[Int]) : Int = {
if (x.isEmpty) return 0
else
return (myListLength(x.tail) + 1)
} //> myListLength: (x: List[Int])Int
// myListLength(L3) //> res1: Int = 31
def listLengthIter(x: List[Int]) : Int = {
def helper(xs: List[Int], n: Int) : Int = {
if (xs.isEmpty) return n
else
return (helper(xs.tail, n + 1))}
return (helper(x, 0))
} //> listLengthIter: (x: List[Int])Int
listLengthIter(L3) //> res2: Int = 31
}