Erlang 哈希树

发布于 2024-07-23 10:23:57 字数 622 浏览 3 评论 0原文

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

您认为问题出在哪里?

谢谢你, 安德烈亚斯

I'm working on a p2p app that uses hash trees.

I am writing the hash tree construction functions (publ/4 and publ_top/4) but I can't see how to fix publ_top/4.

I try to build a tree with 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

The code in question is here:

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

Where do you think the problem is?

Thank You,
Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-07-30 10:23:57

查看您的代码,我可以看到一个问题,该问题会

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

Looking at your code I can see one issue that would generate that particular exception error

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.

In the first function declaration you match against the empty list. In the second declaration you match against a list of length (at least) 2 ([F,S|T]). What happens when FullLevelLen is different from 1 and RestOfLevel is a list of length 1? (Hint: You'll get the above error).

The error would be easier to spot if you would pattern match on the function arguments, perhaps something like:

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) ->
%     ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文