January 19, 2009

Confused by cons

while learning how to use lists:

val oneTwo = List(1, 2)
val threeFour = List(3, 4)
val oneTwoThreeFour = oneTwo :: threeFour //forgot one ":", I really meant to ":::" (concatenate)
val filteredList = oneTwoThreeFour.filter(n => n > 1)

scalac informed me:

Error:Error:line (16)error: value > is not a member of scala.this.Any
val l2 = oneTwoThreeFour.filter(n => n > 1)


Commenting out the offending line and replacing it with

println("oneTwoThreeFour " + oneTwoThreeFour)

yields

oneTwoThreeFour List(List(1, 2), 3, 4)


which clearly shows that the first element is not an Int.

However, the following line compiles:

val filteredList = oneTwoThreeFour.filter(n => n == 1)

probably because '==' is defined for scala.this.Any