Networking Erlang哈希树
我正在开发一个使用哈希树的p2p应用程序 我正在编写哈希树构造函数(publ/4和publ_top/4),但我看不出如何修复publ_top/4 我尝试用publ/1构建一棵树:Networking Erlang哈希树,networking,functional-programming,erlang,p2p,sctp,Networking,Functional Programming,Erlang,P2p,Sctp,我正在开发一个使用哈希树的p2p应用程序 我正在编写哈希树构造函数(publ/4和publ_top/4),但我看不出如何修复publ_top/4 我尝试用publ/1构建一棵树: nivd:publ("file.txt"). prints hashes... ** exception error: no match of right hand side value [67324168] in function nivd:publ_top/4 in call from n
nivd:publ("file.txt").
prints hashes...
** exception error: no match of right hand side value [67324168]
in function nivd:publ_top/4
in call from nivd:publ/1
有关守则如下:
你认为问题出在哪里
谢谢,,
Andreas查看您的代码,我可以看到一个问题,它会生成特定的异常错误
publ_top(_,[],Accumulated,Level) ->
%% Go through the accumulated list of hashes from the prior level
publ_top(string:len(Accumulated),Accumulated,[],Level+1);
publ_top(FullLevelLen,RestofLevel,Accumulated,Level) ->
case FullLevelLen =:= 1 of
false -> [F,S|T]=RestofLevel,
io:format("~w---~w~n",[F,S]),
publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
true -> done
end.
在第一个函数声明中,匹配空列表。在第二个声明中,匹配长度(至少)2的列表([F,S|T]
)。当FullLevelLen
与1不同并且RestOfLevel
是长度为1的列表时会发生什么?(提示:您将得到上述错误)
如果在函数参数上进行模式匹配,则更容易发现错误,例如:
publ_top(_,[],Accumulated,Level) ->
%% Go through the accumulated list of hashes from the prior level
publ_top(string:len(Accumulated),Accumulated,[],Level+1);
publ_top(1, _, _, _) ->
done;
publ_top(_, [F,S|T], Accumulated, Level) ->
io:format("~w---~w~n",[F,S]),
publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
%% Missing case:
% publ_top(_, [H], Accumulated, Level) ->
% ...
查看您的代码,我可以看到一个问题,它会生成特定的异常错误
publ_top(_,[],Accumulated,Level) ->
%% Go through the accumulated list of hashes from the prior level
publ_top(string:len(Accumulated),Accumulated,[],Level+1);
publ_top(FullLevelLen,RestofLevel,Accumulated,Level) ->
case FullLevelLen =:= 1 of
false -> [F,S|T]=RestofLevel,
io:format("~w---~w~n",[F,S]),
publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
true -> done
end.
在第一个函数声明中,匹配空列表。在第二个声明中,匹配长度(至少)2的列表([F,S|T]
)。当FullLevelLen
与1不同并且RestOfLevel
是长度为1的列表时会发生什么?(提示:您将得到上述错误)
如果在函数参数上进行模式匹配,则更容易发现错误,例如:
publ_top(_,[],Accumulated,Level) ->
%% Go through the accumulated list of hashes from the prior level
publ_top(string:len(Accumulated),Accumulated,[],Level+1);
publ_top(1, _, _, _) ->
done;
publ_top(_, [F,S|T], Accumulated, Level) ->
io:format("~w---~w~n",[F,S]),
publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
%% Missing case:
% publ_top(_, [H], Accumulated, Level) ->
% ...
如果你好奇,应用程序在这里描述:如果你好奇,应用程序在这里描述:谢谢!该程序建立了一个哈希树,你的风格提示改进了我的编码。谢谢!该程序构建了一个哈希树,您的风格提示改进了我的编码。