I am stumped on something pretty basic in Scala. I'm working my way through the book "Atomic Scala." I'm not a complete beginner to programming, but what experience I do have is pretty surface-level. No actual development experience, just tinkering around with string manipulation and things at that level.
In the book, one of the exercises (in a section on Parameterized Types) is to define a method which takes a vector as input and gives a list as output, where the list's elements are the same as the vector's elements. The given vector could be of any length.
I don't know of a way to do this without a for loop and without thinking of the list as (at least at first) mutable.
Here's what I tried:
def explicit(v:Vector[Double]):List[Double] = {
var l = List()
for (i <- v) {
l = l :+ i
}
l
}
but it appears there's something wrong with that fourth line. It kicks out an error message.
I have a feeling there's some cool thing you can do without a for loop where you just say right off the bat "val l = List(~~~insert something cool here with rockets or something~~~) but either the book hasn't covered this yet or I just haven't figured it out.
As you can see, I'm at a very basic level here, so please do your best to explain it like I'm five. ;)
Edited to add: I looked ahead a little bit, and it looks like a couple of chapters later I'll be learning about the "map" method, which if I'm skimming accurately, would provide a solution to this problem. But that's later in the book. Any idea how it can be done without map?