I'm running Scala 2.10.2. I want to create a list, then add some elements to the list and expect to see all the elements in the lists when I call the list's name. But I observed something quite weird (At least weird for me since I'm a newbie). Below is the what I tried to do in my sbt console
scala> val l = 1.0 :: 5.5 :: Nil
l: List[Double] = List(1.0, 5.5)
scala> l
res0: List[Double] = List(1.0, 5.5)
scala> l ::: List(2.2, 3.7)
res1: List[Double] = List(1.0, 5.5, 2.2, 3.7)
scala> List(l) :+ 2.2
res2: List[Any] = List(List(1.0, 5.5), 2.2)
scala> l
res3: List[Double] = List(1.0, 5.5)
scala>
First, I created the list l
with 2 elements (1.0 and 5.5). I call l
and get what I expect; the two elements. Now I tried to add another element to the list using :::
which returned a new list with a new list of elements I added (2.2 and 3.7) Sweet! I even checked someone else's code for help: Appending an element to the end of a list in Scala to use a new construct :+
. So at this stage I'm all happy, but I call l
and I get the unexpected: `res3: List[Double] = List(1.0, 5.5)'.
Where are the elements I added? And how do I add these elements correctly so that when I call l
I get a new list with all the stuff I added?