-1

我正在开发一个使用哈希树的 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 nivd:publ/1

有问题的代码在这里:

http://github.com/AndreasBWagner/nivoa/blob/886c624c116c33cc821b15d371d1090d3658f961/nivd.erl

你认为问题出在哪里?

谢谢你,安德烈亚斯

4

1 回答 1

4

查看您的代码,我可以看到一个会产生特定异常错误的问题

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) ->
%     ...
于 2009-06-14T05:21:04.190 回答