如何在 Elixir 中作为副作用打印?

How to print as a side-effect in Elixir?

我正在 Elixir 中重新制作流行的文字游戏“Wordle”。如果用户不输入 5 个字母的单词,我想给他们反馈。到目前为止我得到了这个:

  def play do
    guess = IO.gets("Guess a 5 letter word: ")
    guess = String.trim(guess)
    guess = String.downcase(guess)
    attempt(guess)
  end

  def attempt(@correct_word) do
    "You won!"
  end

  def attempt(guess) when byte_size(guess) != 5 do
    "Guess must be 5 letters"
    play()
  end

  def attempt(guess) when byte_size(guess) == 5 do
    determine_guess_result(guess)
    "You have x guesses left"
  end

问题是这一行永远不会显示:"Guess must be 5 letters" 因为我在之后立即调用 play() 所以它永远不会从函数返回

如何在 Elixir 中实现这一点,我可以在其中显示此行,然后再次调用 play() 为用户提供下一回合?

使用 IO.puts/2,默认情况下打印时会附加一个换行符。

IO.puts("Guess must be 5 letters")
play()