2009-06-13 76 views
0

我正在使用哈希树的p2p应用程序。Erlang哈希树

我正在编写哈希树构造函数(publ/4和publ_top/4),但我看不到如何解决publ_top/4。

我尝试建立与公布/ 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

如果你认为这个问题是什么?

谢谢你, 安德烈亚斯

+0

如果你好奇,应用程序被描述如下:HTTP://wiki.github .com/AndreasBWagner/nivoa – andreasw 2009-06-14 00:47:07

回答

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) -> 
%  ... 
+0

谢谢!该程序构建了一个哈希树,并且您的样式提示改进了我的编码。 – andreasw 2009-06-19 21:12:51