Syntax 将列表传递给函数时Elixir中的算术问题
我对长生不老药和功能性药物还不熟悉。我正在制作一个玩具扑克游戏,以便更熟悉这门语言。编译此代码时,出现以下错误:Syntax 将列表传递给函数时Elixir中的算术问题,syntax,functional-programming,elixir,Syntax,Functional Programming,Elixir,我对长生不老药和功能性药物还不熟悉。我正在制作一个玩具扑克游戏,以便更熟悉这门语言。编译此代码时,出现以下错误: ** (BadArityError) #Function<12.99386804/2 in :erl_eval.expr/5> with arity 2 called with 1 argument ('SK') 这是语言的一部分吗?还是我犯了错误?我发现您的代码有两个问题: 首先,这条线 def burn_cards(current_hand, cards = [
** (BadArityError) #Function<12.99386804/2 in :erl_eval.expr/5> with arity 2
called with 1 argument ('SK')
这是语言的一部分吗?还是我犯了错误?我发现您的代码有两个问题: 首先,这条线
def burn_cards(current_hand, cards = []) do
意味着传入的第二个参数必须是空列表,但不是传入空列表,而是传入一个包含一个元素的列表。我相信你想用这个来代替
def burn_cards(current_hand, cards \\ []) do
这意味着如果省略,默认值为空列表,但也接受包含元素的列表
其次是过滤部分
Enum.filter(current_hand, fn (x, cards) -> x not in cards end)
你不应该在那里有牌。相反,你应该试试看
Enum.filter(current_hand, fn (x) -> x not in cards end)
我发现您的代码存在两个问题: 首先,这条线
def burn_cards(current_hand, cards = []) do
意味着传入的第二个参数必须是空列表,但不是传入空列表,而是传入一个包含一个元素的列表。我相信你想用这个来代替
def burn_cards(current_hand, cards \\ []) do
这意味着如果省略,默认值为空列表,但也接受包含元素的列表
其次是过滤部分
Enum.filter(current_hand, fn (x, cards) -> x not in cards end)
你不应该在那里有牌。相反,你应该试试看
Enum.filter(current_hand, fn (x) -> x not in cards end)
我相信您的问题是传递给Enum.filter/2的匿名函数的第二个参数。 这个函数似乎只能接收一个参数。这就是,我们可以这样做:
iex(3)> pairs = [2, 4]
[2, 4]
iex(4)> Enum.filter([1, 2, 3, 4, 5], fn (x) -> x in pairs end)
[2, 4]
但不是这个:
iex(5)> pairs
[2, 4]
iex(6)> Enum.filter([1, 2, 3, 4, 5], fn (x, pairs) -> x in pairs end)
** (BadArityError) #Function<12.118419387/2 in :erl_eval.expr/5> with arity 2 called with 1 argument (1)
(elixir) lib/enum.ex:2857: Enum.filter_list/2
另外,我还有一个问题:
** (FunctionClauseError) no function clause matching in Hand.burn_cards/2
The following arguments were given to Hand.burn_cards/2:
# 1
['7S', 'S5', 'S4', 'D7', 'H7']
# 2
['7S']
help.exs:16: Hand.burn_cards/2
(elixir) lib/code.ex:376: Code.require_file/2
之所以发生这种情况,是因为匹配卡=[]仅在卡被选中时才成功
还有一个空列表。所以你的函数调用
永远不会匹配。您可能需要定义burn_卡/2,如
或者,如果你真的想确保卡片是一个列表,你可以这样做
希望我能帮忙= 我相信您的问题是传递给Enum.filter/2的匿名函数的第二个参数。 这个函数似乎只能接收一个参数。这就是,我们可以这样做:
iex(3)> pairs = [2, 4]
[2, 4]
iex(4)> Enum.filter([1, 2, 3, 4, 5], fn (x) -> x in pairs end)
[2, 4]
但不是这个:
iex(5)> pairs
[2, 4]
iex(6)> Enum.filter([1, 2, 3, 4, 5], fn (x, pairs) -> x in pairs end)
** (BadArityError) #Function<12.118419387/2 in :erl_eval.expr/5> with arity 2 called with 1 argument (1)
(elixir) lib/enum.ex:2857: Enum.filter_list/2
另外,我还有一个问题:
** (FunctionClauseError) no function clause matching in Hand.burn_cards/2
The following arguments were given to Hand.burn_cards/2:
# 1
['7S', 'S5', 'S4', 'D7', 'H7']
# 2
['7S']
help.exs:16: Hand.burn_cards/2
(elixir) lib/code.ex:376: Code.require_file/2
之所以发生这种情况,是因为匹配卡=[]仅在卡被选中时才成功
还有一个空列表。所以你的函数调用
永远不会匹配。您可能需要定义burn_卡/2,如
或者,如果你真的想确保卡片是一个列表,你可以这样做
希望我能帮忙= 请使用并提倡函数的速记符号:Enum.filtercurrent\u hand,&&1不在卡片中。请使用并提倡函数的速记符号:Enum.filtercurrent\u hand,&&1不在卡片中。