所以,我有一个名为
conquered
的字典。该字典的用途是存储列表的键、值字典,以使用分而治之方法的并发性来搜索值。
代码继续:
-module(divide).
-export([divide_the_cache/4]).
-define(divided, dict:new()).
divide_the_cache(cache, divided, thread_index, acc) ->
case length(cache) =< acc of
thread_index ->
l = lists:merge([lists:nth(thread_index, divided)], [lists:nth(acc, cached)]);
conquered = dict:append(thread_index, l, dict:from_list(divided));
thread_index = if length(l) == 24 -> thread_index = thread_index + 1, end;
divide_the_cache(cache, conquered, thread_index, acc+1);
end.
我得到:
divide.erl:10:69: syntax error before: ';'
% 10| conquered = dict:append(thread_index, l, dict:from_list(divided));
% |
当我不在 dict:append 上使用函数组合时,我得到:
divide.erl:10:36: syntax error before: ';'
% 10| d_list = dict:from_list(divided);
% | ^
我已经查阅了网络上的文档和网站,但可能会遇到一个示例(除了使用 dict:from_list )并按照尝试 repl 的文档进行操作。
可能会出现什么问题?实现我的需求的最佳方法是什么?
预先感谢,卢卡斯。
您的代码有多个问题,其中大多数是语法问题。让我尝试以更“语法正确”的方式重写它。然后,您需要自行修复您想要赋予它的任何语义:
-module(divide).
-export([divide_the_cache/4]).
divide_the_cache(Cache, Divided, ThreadIndex, Acc)
when length(Cache) =< Acc ->
L = lists:merge(
[lists:nth(ThreadIndex, dict:to_list(Divided))],
[lists:nth(Acc, Cache)]
),
Conquered = dict:append(ThreadIndex, L, Divided),
NewThreadIndex =
case length(L) of
24 -> ThreadIndex + 1;
_ -> ThreadIndex
end,
divide_the_cache(Cache, Conquered, NewThreadIndex, Acc+1).
我改变了什么:
Erlang 中的
-define
?divided
宏。Erlang 中的变量以大写字母开头。case length(Cache) =< Acc of ThreadIndex ->
when length(Cache) =< Acc
)。Divided
lists:nth/2
,我使用了dict:to_list/1
。在 Erlang 中,表达式以 ,
;
),函数以 .
结尾。你不应该在 Erlang 中编写 else-less if,所以我将你的 if
case
语句。Erlang 中的变量不能重新绑定,这就是我引入NewThreadIndex