如何向Erlang中的进程列表广播消息?

如何向Erlang中的进程列表广播消息?,erlang,erlang-shell,Erlang,Erlang Shell,我是Erlang的新手,我试图了解如何将消息从一个进程发送到一个进程列表 假设我们有一个数据结构,其中包含一个带有PID的列表。如何使Pid向Pid列表发送消息“M”,其中列表的每个元素都有2个元素:字符串(表示名称)和Pid? 我想到的是: broadcast(P, M, R) -> P ! {self(), friends}, receive {P, Friends} -> P ! {self(), {send_message, {M, R, P, Frien

我是Erlang的新手,我试图了解如何将消息从一个进程发送到一个进程列表

假设我们有一个数据结构,其中包含一个带有PID的列表。如何使Pid向Pid列表发送消息“M”,其中列表的每个元素都有2个元素:字符串(表示名称)和Pid? 我想到的是:

broadcast(P, M, R) ->
  P ! {self(), friends},
  receive
    {P, Friends} ->
  P ! {self(), {send_message, {M, R, P, Friends}}}
  end.

looper({Name, Friends, Messages}) ->
{From, {send_message, {M, R, ID, [{FriendPid, FriendName} | FriendTale]}}} ->
  if R =< 0 ->
        From ! {From, {self(), {ID, M}}},
        looper({Name, [{FriendPid, FriendName} | FriendTale], [{ID, M} | Messages]});
     R > 0  andalso FriendTale =/= []->
       FriendPid ! {From, {send_message, {M, R-1, ID, FriendTale}}},
       looper({Name, FriendTale, [{ID, M} | Messages]})
  end;
 terminate ->
    ok
end.
其中“M”是我想要广播到称为“Friends”的PID列表的消息,R只是一个整数

R基本上是一个整数,表示消息应该走多远

e.g. 0 = broadcast the message to self,
     1 = broadcast the message to the friends of the pid,
     2 = broadcast the message to the friends of the friends of the pid and so on...
设置Pid并设置Pid之间的“友谊”后,我从终端获得的是:

1> f().
ok
2> c(facein).
facein.erl:72: Warning: variable 'From' is unused
{ok,facein}
3> {Message, Pid} = facein:start({"Bjarki", [], []}).
{ok,<0.186.0>}
4> {Message, Pid2} = facein:start({"Bjarki2", [], []}).
{ok,<0.188.0>}
5> facein:add_friend(Pid,Pid2).
ok
6> facein:broadcast(Pid,"hello",1).

=ERROR REPORT==== 5-Oct-2014::12:12:58 ===
Error in process <0.186.0> with exit value: {if_clause,[{facein,looper,1,[{file,"facein.erl"},{line,74}]}]}

{<0.177.0>,{send_message,{"hello",1,#Ref<0.0.0.914>}}}
1>f()。
好啊
2> c(面内)。
facein.erl:72:警告:变量“From”未使用
{好的,facein}
3> {Message,Pid}=facein:start({“Bjarki”,[],[]})。
{好的,}
4> {Message,Pid2}=facein:start({“Bjarki2”,[],[]})。
{好的,}
5> facein:添加朋友(Pid,Pid2)。
好啊
6> facein:广播(Pid,“你好”,1)。
=错误报告===2014年10月5日::12:12:58===
使用退出值进行处理时出错:{if_子句,[{facein,looper,1,[{file,“facein.erl”},{line,74}]}
{,{发送消息,{“你好”,1,{Ref}}
任何帮助都将不胜感激。 谢谢


编辑


添加brodcast函数后。您正在接收发送到
looper
函数的是
friends
atom。你们不能在atom上理解列表,只能在列表上理解。这就是为什么当你尝试使用<代码> > <代码> >你可以添加<代码>循环< /代码>定义(你的参数是如何命名为精确的)。它有一个方便的功能,可以让您订阅某个事件的进程,然后向该事件的所有订阅者发布消息。感谢您的回复,但我重新编辑了我的问题,使其更清楚:)我希望这有帮助。谢谢你的时间。我加了一个完整的活套,以防它的其他部分出了问题。我还可以更好地解释每个变量是什么。:)
1> f().
ok
2> c(facein).
facein.erl:72: Warning: variable 'From' is unused
{ok,facein}
3> {Message, Pid} = facein:start({"Bjarki", [], []}).
{ok,<0.186.0>}
4> {Message, Pid2} = facein:start({"Bjarki2", [], []}).
{ok,<0.188.0>}
5> facein:add_friend(Pid,Pid2).
ok
6> facein:broadcast(Pid,"hello",1).

=ERROR REPORT==== 5-Oct-2014::12:12:58 ===
Error in process <0.186.0> with exit value: {if_clause,[{facein,looper,1,[{file,"facein.erl"},{line,74}]}]}

{<0.177.0>,{send_message,{"hello",1,#Ref<0.0.0.914>}}}
broadcast(P, M, R) ->
  P ! {self(), {send_message, {M, R, P, friends}}}.
receive
  {From, {send_message, {M, R, ID, Friends}}} when is_integer(R) ->
     %%  ...