Home > front end >  How do "variables" work with recursion in Elixir?
How do "variables" work with recursion in Elixir?

Time:08-29

I am an absolute beginner in Elixir and I tried toying around with recursion by making a function like this:

  def player_choice() do
    choice = IO.gets("Choose rock, paper or scissors: ") |> String.trim()

    unless String.downcase(choice) in ["rock", "paper", "scissors"] do
      IO.puts("\nPlease insert a valid move")
      choice = player_choice()
    end

    choice
  end

So if someone inputs a value thats not expected the function should continue asking for an input. But if the first input is something unexpected like "no", and the next input is "rock" the function will return "no". Why does this happen, shouldn't "choice" be the reassigned value?

CodePudding user response:

The problem is that you redefine a new variable choice in your internal unless, masking the outer one (in Elixir, you don't "reassign"). The compiler warns you:

variable "choice" is unused (there is a variable with the same name in the context

A better solution would be something like:

def player_choice() do
  choice = IO.gets("Choose rock, paper or scissors: ") |> String.trim() |> String.downcase
  if choice in ["rock", "paper", "scissors"] do
     choice
  else
     IO.puts("\nPlease insert a valid move")
     player_choice()
  end
end
  • Related