0

I am trying to add some user input Strings to an immutable List with tail recursion and then add this immutable List to a MutableList which is defined. Somehow this immutable List isn't getting prepended to my MutableList. Why is that so?

EDIT: Updated the code. Now its working!

val list = scala.collection.mutable.MutableList[String]()

  def exerciseSelector() {
    val user = scala.io.StdIn.readLine("go:")
    user match {
      case "add" => val tempList = scanInput(List[String]())
                    if (!tempList.isEmpty) list ++= tempList.get ; exerciseSelector()
      case "print" => println(list)
      case "exit" => sys.exit()
    }
  }

    def scanInput(acc: List[String]): Option[List[String]] = {
    val input = scala.io.StdIn.readLine("User input:")
    input match {
    case "stop" => Option(acc)
    case input: String => scanInput(input :: acc)
  }
}

exerciseSelector()

1 Answer 1

3

You have two mutually-recursive functions exerciseSelector and scanInput, which will only ever result in list being updated if scanInput returns, which ultimately can only happen after an input of "exit", which calls sys.exit(), quitting altogether before the return from scanInput can happen, and so before list ever gets updated.

Any input of "print" will be printing out an empty list, as list never gets a chance to be updated without sys.exit() being called first.

Depending on what exactly you are trying to achieve, I suspect just dropping out the co-recursive call to exerciseSelector() on receiving a "stop" input will fix your code (or get you a lot further towards your intended result).

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much now its working just fine!! So let me just summarize this up again: If i remove the exerciseSelector() call at the "stop" match case then the list can be returned, else the list isnt even returned and I am leaving the list behind in the scanInput() because i first called the exerciseSelector() and then returned the list???
@ArmaGeddon You are "leaving the list behind", as you put it, in scanList, because the call to exerciseSelector() in front of the to-be-returned Option(acc) will inevitably result in a call to sys.exit() - ending the program - before completing. Also, you probably shouldn't edit your question so that the code now works, or people reading it may not understand what the problem was nor how the solution fixed it (at least, not without looking at the history of edits).
Okay, so I missunderstood the behaviour of the case here becuase I thought even when I am calling the exerciseSelector() and returning the list back it will do both things but now its clear to me thanks! Yes I edited it but last time another user told me not to "answer my question" or did he meant only not to do that if I am having further questions?
@ArmaGeddon I'm not sure what (s)he might have meant.
Never mind... For the next time, I will do it by just posting another answer than editing my question. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.