object Demo { def disp[A](a : List[A]) { for(e <- a) printf(s"$e ") println } def doubleNum(a : Int) : Int = 2*a def main(args : Array[String]) { var a = List(); // fill var b = List.fill(10)(1); disp(b) // range var c = List.range(1, 10); // 1, 2, 3, 4, 5, 6, 7, 8, 9 disp(c) // tabulate var d = List.tabulate(10)(n => n*n) disp(d) // reverse disp(d.reverse) } }
object Demo { def disp[A](a : List[A]) { for(e <- a) printf(s"$e ") println } def doubleNum(a : Int) : Int = 2*a def main(args : Array[String]) { val a = Nil // empty list val b : List[Int] = List(1, 2, 3, 4) var c : List[Int] = List(5, 6, 7, 8) println("Head: "+b.head) // head println("Tail: "+b.tail) // tail disp(10 :: b) // attach a nubmer before head disp(b :+ 10) // append a number after tail disp(b ::: c) // concate two lists println(b(0)) // access list with index println(b.size) // size println(b.max) // max println(b.min) // min println(b.sum) // sum println(b.mkString("|")) // mkString disp(b.sortWith(_.compareTo(_) < 0)) disp(b.map(doubleNum)) // map println(b.reduce(_+_)) // reduce } }