为什么这个函数调用在Elixir中不匹配?

时间:2018-04-03 15:05:35

标签: pattern-matching elixir

这个功能怎么样?

  @spec of_rna(String.t()) :: {atom, list(String.t())}
  def of_rna(rna) do
        data = rna |> String.codepoints 
                   |> Enum.chunk_every(3)
                   |> Enum.map(fn(x) -> Enum.join(x) end)
        do_rna(data, [])
  end

  def do_rna([head | tail] , Result) do
        case  @proteins[head] do
                STOP -> {:ok, Result}
                _ -> do_rna(tail, Result ++ [@proteins[head]])
        end
  end

我正在使用的测试是:

test "stops translation if stop codon present" do
    strand = "AUGUUUUAA"
    assert ProteinTranslation.of_rna(strand) == {:ok, ~w(Methionine Phenylalanine)}
  end

我看到的错误是:

1) test stops translation if stop codon present (ProteinTranslationTest)
     protein_translation_test.exs:67
     ** (FunctionClauseError) no function clause matching in ProteinTranslation.do_rna/2

     The following arguments were given to ProteinTranslation.do_rna/2:

         # 1
         ["AUG", "UUU", "UAA"]

         # 2
         []

     code: assert ProteinTranslation.of_rna(strand) == {:ok, ~w(Methionine Phenylalanine)}
     stacktrace:
       protein_translation.exs:37: ProteinTranslation.do_rna/2
       protein_translation_test.exs:69: (test)

我不明白为什么函数调用不匹配。请帮忙。

1 个答案:

答案 0 :(得分:4)

您可能打算在result中写Result而不是do_rna。以大写字母开头的标识符是Elixir中的原子。对于Result,该子句仅在第二个值为字面Result:"Elixir.Result"时匹配。