簡體   English   中英

函數子句錯誤Erlang

[英]Function clause error Erlang

我試圖了解erlang中的過程通信。 在這里,我有一個主流程和五個朋友流程。 如果朋友向其他5個人中的任何一個發送消息,則他們必須回復。 但是主人應該意識到這一切。 我正在粘貼下面的代碼。

-module(prog).
-import(lists,[append/2,concat/1]).

-import(maps,[from_lists/1,find/2,get/2,update/3]).
-import(string,[equal/2]).
-import(file,[consult/1]).
-export([create_process/1,friends/4, master/1, main/0,prnt/1]).     



%% CREATE PROCESS 
create_process([])->ok;
create_process([H|T])->
    {A,B} = H,
    Pid = spawn(prog,friends,[B,self(),0,A]),
    register(A,Pid),
    create_process(T).


%% FRIENDS PROCESS
friends(Msg, M_pid, State, Self_name)->
    S = lists:concat([Self_name," state =",State,"\n"]),
    io:fwrite(S),
    if
        State == 0 ->
            timer:sleep(500),
            io:fwrite("~p~n",[Self_name]),
            lists:foreach(fun(X) -> whereis(X)!{Self_name,"intro",self()} end, Msg),
            friends(Msg, M_pid, State + 1, Self_name);
        State > 0 ->
            receive
                {Process_name, Process_msg, Process_id} -> 
                    I = equal(Process_msg,"intro"),
                    R = equal(Process_msg,"reply"),
                    XxX = lists:concat([Self_name," recieved ",Process_msg," from ",Process_name,"\n"]),
                    io:fwrite(XxX),
                    if
                        I == true ->
                            io:fwrite("~p~n",[whereis(Process_name)]),
                            M_pid!{lists:concat([Self_name," received intro message from ", Process_name , "[",Process_id,"]"]), self()},
                            io:fwrite(I),
                            whereis(Process_name)!{Self_name, "reply",self()},

                            friends(Msg, M_pid, State + 1, Self_name);
                        R == true ->
                            M_pid!{lists:concat([Self_name," received reply message from ", Process_name , "[",Process_id,"]"]), self()},
                            io:fwrite(R),
                            friends(Msg, M_pid, State + 1, Self_name)
                    end
            after
                1000->
                    io:fwrite(lists:concat([Self_name," has received no calls for 1 second, ending..."]))
            end

    end.


master(State)->
    receive
        {Process_message, Process_id} ->
            io:fwrite(Process_message),
            master(State+1)
    after
        2000->
            ok
    end.


main() ->
    B = [{john, [jill,joe,bob]},
{jill, [bob,joe,bob]},
{sue, [jill,jill,jill,bob,jill]},
{bob, [john]},
{joe, [sue]}],
    create_process(B),
    io:fwrite("~p~n",[whereis(sue)]),

    master(0).

我認為friends()函數中的代碼行

M_pid!{lists:concat([Self_name," received intro message from ", Process_name , "[",Process_id,"]"]), self()}

是錯誤的原因,但我不明白為什么。 M_pid是已知的,我將所有信息串聯起來並發送給master,但我感到困惑,為什么它不起作用。

我收到的錯誤如下:

Error in process <0.55.0> with exit value: {function_clause,[{lists,thing_to_list,
                         [<0.54.0>],
                         [{file,"lists.erl"},{line,603}]},
                  {lists,flatmap,2,[{file,"lists.erl"},{line,1250}]},
                  {lists,flatmap,2,[{file,"lists.erl"},{line,1250}]},
                  {prog,friends,4,[{file,"prog.erl"},{line,45}]}]}

我不知道是什么導致錯誤。 很抱歉問到菜鳥問題,謝謝您的幫助。

Dogbert發現的示例:

-module(my).
-compile(export_all).

go() ->
    Pid = spawn(my, nothing, []),
    lists:concat(["hello", Pid]).


nothing() -> nothing.

在外殼中:

2> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

3> my:go().
** exception error: no function clause matching 
                    lists:thing_to_list(<0.75.0>) (lists.erl, line 603)
     in function  lists:flatmap/2 (lists.erl, line 1250)
     in call from lists:flatmap/2 (lists.erl, line 1250)
4> 

但:

-module(my).
-compile(export_all).

go() ->
    Pid = spawn(my, nothing, []),
    lists:concat(["hello", pid_to_list(Pid)]).

nothing() -> nothing.

在外殼中:

4> c(my).  
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

5> my:go().
"hello<0.83.0>"

erl文檔

concat(Things) -> string()
    Things = [Thing]
    Thing = atom() | integer() | float() | string()

您提供給concat()的列表必須包含原子,整數,浮點數或字符串。 pid既不是原子,整數,浮點數也不是字符串,因此pid不能與concat()一起使用。 但是, pid_to_list()返回一個字符串:

pid_to_list(Pid) -> string()
    Pid = pid()

如您所見,pid有其自己的類型:pid()。

我運行了您的代碼。 出錯的地方是將Process_id (類型為pid() )傳遞給lists:concat/1

讓我們嘗試了解此錯誤:

{function_clause,[{lists,thing_to_list,
                         [<0.84.0>],
                         [{file,"lists.erl"},{line,603}]},
                  {lists,flatmap,2,[{file,"lists.erl"},{line,1250}]},
                  {lists,flatmap,2,[{file,"lists.erl"},{line,1250}]},
                  {prog,friends,4,[{file,"prog.erl"},{line,39}]}]}

它聲明函數lists:thing_to_list/1沒有定義(請參見錯誤日志中的單詞function_clause ),該定義接受類型為pid()的參數,如[<0.84.0>]

字符串用erlang中的列表表示,這就是為什么我們使用list:concat / 1的原因。

正如@ 7stud指出的那樣,這些是可以根據文檔傳遞給lists:concat/1的有效類型:

atom() | integer() | float() | string()

下一行有2次出現。 修復它們,您就可以進行以下工作:

不正確的代碼:

M_pid!{lists:concat([Self_name," received intro message from ", Process_name , "[",Process_id,"]"]), self()},

更正的代碼

M_pid!{lists:concat([Self_name," received intro message from ", Process_name , "[",pid_to_list(Process_id),"]"]), self()},

注意函數erlang:pid_to_list/1 根據文檔,該函數接受pid()類型並將其作為string()返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM