我還是新編程和全新erlang(2周新手!)。我編輯稍微,至少它會編譯和運行。但我仍然無法弄清楚將結果發送到「加工程序」以加入所有單獨結果的概念。Erlang新手 - 併發和消息傳遞
它分裂併發送接收到的「塊」來計算塊。只是不知道如何讓所有這些過程加入他們的個人成果。我有點理解下面的概念,但不知道這是如何實現的。我一直在嘗試了很多天時間,對得到它了這一點,但不能讓它沒有得到錯誤或綁定變量做任何事情,等
-module (ccharcount1d).
-compile(export_all).
load(F)->
{ok, Bin} = file:read_file(F),
List=binary_to_list(Bin),
Ls=string:to_lower(List),
Length=round(length(List)/20),
Collect_Results = spawn(ccharcount1d, collect_results, []),
Sl=split(Ls,Length),
io:fwrite("Loaded, Split, and sent to multiple processes~n").
%%splits txt file into "chunks" and sends those "chunks" to be processed
split([],_)->[];
split(List,Length)->
S1=string:substr(List,1,Length),
case length(List) > Length of
true->S2=string:substr(List,Length+1,length(List)),
Process_Split = spawn(ccharcount1d,receive_splits,[]),
Process_Split ! {self(), S1};
false->S2=[],
Process_Split = spawn(ccharcount1d,receive_splits,[]),
Process_Split ! {self(), S1}
end,
[S1]++split(S2,Length).
%%recieves the split "chunks" and counts the results
receive_splits()->
receive
{From, S1} ->
Result=go(S1)
%Collect_Results ! Result
end.
collect_results()->
receive
{Process_Split, Result} ->
Result=join([],Result)
end.
join([],[])->[];
join([],R)->R;
join([H1 |T1],[H2|T2])->
{C,N}=H1,
{C1,N1}=H2,
[{C1,N+N1}]++join(T1,T2).
count(Ch, [],N)->N;
count(Ch, [H|T],N) ->
case Ch==H of
true-> count(Ch,T,N+1);
false -> count(Ch,T,N)
end.
go(L)->
Alph=[$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u,$v,$w,$x,$y,$z],
rgo(Alph,L,[]).
rgo([H|T],L,Result)->
N=count(H,L,0),
Result2=Result++[{[H],N}],
rgo(T,L,Result2);
rgo([],L,Result)-> Result.
*我還是新的編程和全新的erlang(2周新手!)*在我看來,糟糕的主意。學習python。花一年的時間玩它,然後決定你想從那裏去哪裏。 – 7stud