The following code can be compiled
def isEven(a:Int)=
if (a%2==0) true else false
def main(args: Array[String]) {
List(1, 10) filter isEven foreach println
but if I change to following ( List(1,10) –> List(1 to 10))
def isEven(a:Int)=
if (a%2==0) true else false
def main(args: Array[String]) {
List(1 to 10) filter isEven foreach println
}
What’s difference between List(1,10) and List(1 to 10) ?
List(1, 2)is simply a list with twoIntelements: 1 and 2. The expression1 to 10creates aRangeinstance, soList(1 to 10)is a list with one element: aRange.