Thanks!
What should happen if you find anything other than 1 or 2 in the list?
Hint: your splitList()
function should have three parameters: the input, the current state of the 1
list, and the current state of the 2
list. Given that, in the function, figure out whether you are done (in which case, return your results); otherwise, call splitList()
recursively, with adjusted values to each of the parameters.
Not to make your life harder, but a common way to implement this as a higher order function would be to pass a predicate function (takes an element and returns true or false), and use that to determine to which list an element will be added. This is how Scala’s built-in ‘partition’ function works; it might be worth looking at that for hints if you feel stuck.
I know this does not answer your question, but you can split those lists, functionally, without recursion. I guess if the list were infinite, it would better be solved recursively. If you’re interested in recursion in Scala, answers are only a google away with simply “Scala recursion”(no quotes). Read items that include the reference to @tailrec. This sounds a bit like homework. But if you want to do this in Scala and really understand it, don’t use mutable.List or var.
Best and just IMO
Jeff