简体   繁体   English

Elixir :( FunctionClauseError)没有函数子句匹配

[英]Elixir: (FunctionClauseError) no function clause matching

I've made this elixir module that should print each number, "counting" up to the number you give it. 我制作了这个elixir模块,应该打印每个数字,“计数”到你给它的数字。

defmodule Count do
  def to(n) do
    m = 1
    _to(n, m)
  end
  defp _to(n, m) when (m <= n) do
    IO.puts "#{m}"
    x = m + 1
    _to(n, x)
  end
end

...but when I run it, it performs exactly as expected except that it throws this error at the end. ...但是当我运行它时,它的执行完全符合预期,但它会在最后抛出此错误。 What is happening here? 这里发生了什么?

iex(1)> Count.to 5  
1
2
3
4
5
** (FunctionClauseError) no function clause matching in Count._to/2
count.exs:6: Count._to(5, 6)
iex(1)>

Thank you for any help. 感谢您的任何帮助。

Elixir doesn't silently ignore a function call if none of the clauses match -- you get a FunctionClauseError . 如果没有子句匹配,Elixir不会默默地忽略函数调用 - 你得到一个FunctionClauseError In this case, when m > n , no function clause in _to matches, so Elixir throws that error. 在这种情况下,当m > n_to中没有函数子句匹配,因此Elixir抛出该错误。 You need to add another version of _to which accepts any m and n (or you could add a when m > n there, if you want) and does nothing. 您需要添加另一个版本的_to ,它接受任何mn (或者你可以when m > n那里添加when m > n ,如果你愿意的话)并且什么都不做。

defp _to(n, m) when (m <= n) do
  IO.puts "#{m}"
  x = m + 1
  _to(n, x)
end
defp _to(n, m) do
end

You did not handle the case when m > n , but you are still calling it. m > n时你没有处理这种情况,但你还在调用它。 You either dont call it, or have a function definition that handles this case. 你要么不要调用它,要么有一个处理这种情况的函数定义。

  defp _to(n, m) when (m <= n) do
    IO.puts "#{m}"
    x = m + 1
    _to(n, x)
  end

  defp _to(n, m), do: IO.puts "Completed Counting" end

This shortens it up after looking at the answers given here. 在查看此处给出的答案后,这会缩短它。 The explanation for the answer was a great one, thank you guys. 答案的答案很棒,谢谢你们。

defmodule Count do
  def to(n, m \\ 1)
  def to(n, m) when n == m, do: IO.puts "#{n}"
  def to(n, m) do
    IO.puts "#{m}"
    to(n, m + 1)
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Phoenix 1.3 Elixir jsonapi(FunctionClauseError)在render / 2中没有匹配的函数子句 - Phoenix 1.3 Elixir jsonapi (FunctionClauseError) no function clause matching in render/2 Elixir Pheonix通道(FunctionClauseError)ProjectName.ModuleName.handle_in / 3中没有匹配的功能子句 - Elixir Pheonix Channels (FunctionClauseError) no function clause matching in ProjectName.ModuleName.handle_in/3 (FunctionClauseError)Access.get / 3中没有匹配的功能子句 - (FunctionClauseError) no function clause matching in Access.get/3 (FunctionClauseError) 没有 function 子句匹配 CheckersgameWeb.GamesChannel.handle_in/3 - (FunctionClauseError) no function clause matching in CheckersgameWeb.GamesChannel.handle_in/3 Elixir-插件-无功能子句匹配 - Elixir - Plug - no function clause matching 无法运行任何混合命令-**(FunctionClauseError)IO.chardata_to_string / 1中没有匹配的功能子句 - Can't run any mix command - ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1 (FunctionClauseError)Access.get / 3中没有匹配的功能子句-单独的问题,因为该主题中的其他问题被删除 - (FunctionClauseError) no function clause matching in Access.get/3 - separate question because additional ones in the topic are being deleted Phoenix 1.4升级-(FunctionClauseError)Phoenix.Socket中没有匹配的函数子句。__terminate __ / 2 - Phoenix 1.4 Upgrade - (FunctionClauseError) no function clause matching in Phoenix.Socket.__terminate__/2 Elixir Phoenix:控制器测试中出现奇怪的“无功能子句匹配”错误 - Elixir Phoenix: Strange “no function clause matching” error in Controller Test Ecto 迁移更改表 ** (FunctionClauseError) 没有函数子句匹配 - Ecto migration alter table ** (FunctionClauseError) no function clause matches
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM