erlang - 为什么我在这个从 Erlang 翻译的 Elixir 程序中收到 FunctionClauseError ("no function clause matching")?

标签 erlang elixir variable-names

**所以,我以前使用过 Erlang,并且对它非常满意。我只是想学习 Elixir。

我最近试图将一个“懒惰的餐饮服务商”示例翻译成 Elixir,但我很困惑为什么它要么不编译,要么编译时出现警告并且不起作用。我在这里缺少什么;有任何想法吗? erlang代码和'run'如下:**

jps@GRASSKEET ~/dev/erlang
$ cat cater.erl

    -module(cater).
    -export([cater/1]).
    cater(0) -> 1;
    cater(N) when N>0 -> N + cater(N-1).

jps@GRASSKEET ~/dev/erlang
$ erl
Eshell V6.3  (abort with ^G)
1> c("cater.erl").
{ok,cater}
2> cater:cater(10).
56
3>*

当我像这样编写 Cater.ex 时,它会出现一个对我来说没有意义的错误:

jps@GRASSKEET ~/dev/elix
$ cat Cater.ex

    defmodule Cater do
      def cut(0), do: 1
      def cut(N) when N>0, do: N + cut(N-1)
    end

jps@GRASSKEET ~/dev/elix
$ iex
Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("Cater.ex")
Cater.ex:1: warning: redefining module Cater
Cater.ex:3: warning: this expression will fail with ArithmeticError
[Cater]
iex(2)> Cater.cut(10)
** (FunctionClauseError) no function clause matching in Cater.cut/1
    Cater.ex:2: Cater.cut(10)
iex(2)>

最佳答案

只是为了扩展问题以及您收到错误的原因:

大写的标识符被视为原子的别名。例如:

iex(1)> N == :Elixir.N
true

所以当你有以下代码时:

iex(1)> defmodule Test do
...(1)> def foo, do: IO.puts "testing"
...(1)> end
iex(2)> Test.foo
testing

和说的一样

iex(3)> :Elixir.Test.foo
testing

由于大写标识符被视为符号,因此您实际上编写了以下程序:

defmodule Cater do
  def cut(0), do: 1
  def cut(:Elixir.N) when :Elixir.N>0, do: :Elixir.N + cut(:Elixir.N-1)
end

这是有效的,因为您可以对参数列表中的原子进行模式匹配,并且 :Elixir.N > 0 是一个有效的表达式。

iex(1)> N > 0
true

考虑以下 Elixir 程序和输出:

iex(1)> defmodule Cater do
...(1)> def cut(0), do: IO.puts "cut(0)"
...(1)> def cut(N) when N > 0, do: IO.puts "cut(N)"
...(1)> end
iex(2)> Cater.cut(0)
cut(0)
iex(3)> Cater.cut(N)
cut(N)
iex(4)> Cater.cut(:Elixir.N)
cut(N)
iex(5)> Cater.cut(1)
** (FunctionClauseError) no function clause matching in Cater.cut/1
    iex:2: Cater.cut(1)

因此,您收到看似奇怪的错误消息 no function Clause matches in Cater.cut/1 的原因是,您的程序在技术上没有任何问题(除了 的实际函数体) >cut(N)——它会引发一个 ArithmeticError,Elixir 在编译时会警告您);它完全有效,只是没有做您想要它做的事/您认为它做的事。

关于erlang - 为什么我在这个从 Erlang 翻译的 Elixir 程序中收到 FunctionClauseError ("no function clause matching")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813039/

相关文章:

elixir - Ecto 查询 - jsonb 列中的 order_by 字段

elixir - httproxy 漏洞是否适用于 Elixir 应用程序?

R:生成变量名称,评估函数列表中的函数,并将这些值分配给循环中生成的变量名称

c++ - Objective-C:避免_作为局部变量名的技术原因?

erlang - 为什么第二次调用 receive 没有在 Erlang shell 中检索消息?

mysql - add_rosteritem 不能在 ejabberd 服务器中使用 xml_rpc

elixir - 在没有 Ecto 的情况下使用 Phoenix 框架

database - 具有非描述性变量名称的可维护代码

erlang - 哪个更可取? guard 还是案件?

algorithm - 有人能解释一下为什么下面的 LIS 算法不是 O(n) 吗?