gpt4 book ai didi

Erlang "badarg",不知道如何处理

转载 作者:行者123 更新时间:2023-12-04 22:43:03 26 4
gpt4 key购买 nike

在开始之前,我已经 checkout Handle badarg in Erlang
但是我的未定义检查仍然没有成功,所以我删除了它们。

我正在构建一个虚拟银行流程,当客户对银行流程进行余额查询检查时,程序退出,说:

Error in process <0.373.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}

=ERROR REPORT==== 26-Oct-2016::13:34:57 ===
Error in process <0.379.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}

=ERROR REPORT==== 26-Oct-2016::13:34:57 ===
Error in process <0.375.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}

=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.377.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}
<0.378.0> Balance request: 54>
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.378.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,39}]}]}
<0.372.0> Balance request: 54>
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.372.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,39}]}]}

模拟代码在这里:
-module(bankSim).
-export([start/0, negativeOrPositive/0, sleep/1, generate_rand_int_list/3, generate_rand/2, client/3, clientSpawn/2, bank/2]).

negativeOrPositive() ->
M = rand:uniform(),
if
M =< 0.5 ->
1;
true ->
-1
end.

sleep(T) ->
receive
after T -> ok
end.

generate_rand_int_list(N,StartVal,Lim) ->
lists:map(fun (_) -> (rand:uniform(Lim-StartVal) + StartVal)* negativeOrPositive() end, lists:seq(1,N)).

generate_rand(StartVal, Lim) ->
rand:uniform(Lim-StartVal) + StartVal.

client([], _ , BankID) ->
BankID ! {goodbye};
client([H|T], Count, BankID) ->
BankID ! {transaction, self(), H},
receive
{Amount, Balance, Success} ->
io:format("Client: ~w, Amount requested: ~w, Bank Balance: ~w, Transaction successful ~w ~n",[self(), Amount, Balance, Success]);
{ X } ->
io:format("The balance is ~w ~n", [X])
end,
sleep(generate_rand(500, 1500)),
Mod = Count rem 5,
if
Mod == 0 ->
io:format("~w Balance request: ",[ self() ]),
BankID! {balance, self()};
true ->
ok
end,
client(T, Count + 1, BankID).

clientSpawn(0, _) ->
io:format("Finished spawning clients ~n",[]);
clientSpawn(N, BankID) ->
spawn(bankSim, client, [ generate_rand_int_list( generate_rand(10, 20), 0, 100) , 1, BankID] ),
clientSpawn(N-1, BankID).


bank(Balance, 0) ->
io:format("Banking simulation ended with a final balance of ~w ~n", [Balance]),
io:format("simulation completed ~n", []);
bank(Balance, NumClients) ->
receive
{balance, Client} ->
Client ! {Balance};
{transaction, Client, Amount} ->
NewBalance = Balance + Amount,
if
NewBalance =< 0 ->
Client ! {Amount, NewBalance, no},
bank(Balance, NumClients);
NewBalance > 0 ->
Client ! {Amount, NewBalance, yes},
bank(NewBalance, NumClients);
true ->
io:format("This will never be printed")
end;
goodbye ->
NewNumClients = NumClients - 1,
bank(Balance, NewNumClients)
end.


start()->
N = generate_rand(2,10),
register(bank ,spawn(bankSim, bank, [generate_rand(2000,3000), N])),
clientSpawn(N,bank).

任何帮助,将不胜感激。

最佳答案

Erlang 文档会告诉你 badarg意味着 "The argument is of wrong data type, or is otherwise badly formed."我从来没有觉得很清楚。

Dogbert 指出您错过了对 bank/2 的递归调用。在你的一种情况下。这是您的badarg的来源s——但为什么呢?

一直想着badarg意思是“发送到 BIF 的错误参数”。如果你调用一个普通的 Erlang 函数,你会得到一个 function_clause错误,意味着没有与您的数据匹配的函数子句。

您收到的错误消息列出了两个不同的行号。一个是为线

BankID ! {transaction, self(), H},

另一个为
BankID! {balance, self()};

不好的论点是 BankID .之所以不好,是因为它是一个原子, bank .在您的 start/0函数,您生成一个新的银行进程,然后将该进程的 PID 注册为 bank .然后在您的客户端中,您将消息发送到 bank atom 并让 Erlang 将其解析为注册的 PID。

它可以工作一段时间,但是当您遇到失踪回避案件时,您的 bank进程终止。现在 bank后面没有人注册atom,并且发送操作符会因为它无法发送到已失效的名称而崩溃。

如果您将代码更改为
Bank = spawn(bankSim, bank, [generate_rand(2000,3000), N]),
clientSpawn(N, Bank).

那么你就不会得到 badarg ,因为将消息发送到没有人运行的 PID 并不是错误。您可能会喜欢在不修复 bank 的情况下进行尝试。循环,看看会发生什么。

关于Erlang "badarg",不知道如何处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40268836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com