【问题标题】:Erlang Hash Tree二郎哈希树
【发布时间】:2010-11-02 18:03:06
【问题描述】:

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

你认为问题出在哪里?

谢谢你, 安德烈亚斯

【问题讨论】:

标签: networking functional-programming erlang p2p sctp


【解决方案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(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) ->
%     ...

【讨论】:

  • 谢谢!该程序构建了一个哈希树,您的样式提示改进了我的编码。
猜你喜欢
  • 1970-01-01
  • 2010-12-06
  • 2010-09-27
  • 2023-03-31
  • 2016-10-03
  • 2011-06-18
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多