Reputation: 443
I am having trouble understanding Scala lists. I just want to create a list of lists: List(list(1,2), List(3,4)) In the REPL I am trying:
val list= List()
val lt = List(1,2)
val ls = List(3,4)
list::lt resolves to - res0: List[Any] = List(List(), 1, 2)
list::ls resolves to - res1: List[Any] = List(List(), 3, 4)
I'm coming from java and have never programmed functionally. I am just not getting it.
Thanks for any help!!
Upvotes: 1
Views: 74
Reputation: 39587
You'll want to read the book, where it explains that cons ::
prepends to the thing on the right.
one way to initialize new lists is to string together elements with the cons operator, with Nil as the last element.
scala> List(1,2) :: Nil
res1: List[List[Int]] = List(List(1, 2))
The book also explains about operators ending in a colon.
If you stick a List[Nothing]
on the front of a List[Int]
, you get the List[Any]
you witnessed.
Upvotes: 6