是什么导致Erlang运行时错误{undef,[{rand,uniform,[2],[]},...]}?

时间:2019-03-28 22:52:04

标签: erlang

在执行Tarry分布式算法的实现时,发生了一个我不知道如何解决的问题:包含错误{undef,[{rand,uniform,[2],[]}的崩溃。我的模块如下:

-module(assign2_ex).
-compile(export_all).

%% Tarry's Algorithm with depth-first version
start() ->
    Out = get_lines([]),
    Nodes = createNodes(tl(Out)),
    Initial = lists:keyfind(hd(Out), 1, Nodes),
    InitialPid = element(2, Initial),
    InitialPid ! {{"main", self()}, []},
    receive
        {_, List} ->
            Names = lists:map(fun(X) -> element(1, X) end, List),
            String = lists:join(" ", lists:reverse(Names)),
            io:format("~s~n", [String])
    end.

get_lines(Lines) ->
    case io:get_line("") of
        %% End of file, reverse the input for correct order
        eof -> lists:reverse(Lines);
        Line ->
            %% Split each line on spaces and new lines
            Nodes = string:tokens(Line, " \n"),
            %% Check next line and add nodes to the result
            get_lines([Nodes | Lines])
    end.

%% Create Nodes
createNodes(List) ->
    NodeNames = [[lists:nth(1, Node)] || Node <- List],
    Neighbours = [tl(SubList) || SubList <- List],
    Pids = [spawn(assign2_ex, midFunction, [Name]) || Name <-NodeNames],                         
    NodeIDs = lists:zip(NodeNames, Pids),
    NeighbourIDs = [getNeighbours(N, NodeIDs) || N <- lists:zip(NodeIDs,   Neighbours)],
    [Pid ! NeighbourPids || {{_, Pid}, NeighbourPids} <- NeighbourIDs],
    NodeIDs.

getNeighbours({{Name, PID}, NeighboursForOne}, NodeIDs) ->
    FuncMap = fun(Node) -> lists:keyfind([Node], 1, NodeIDs) end,
    {{Name, PID}, lists:map(FuncMap, NeighboursForOne)}.

midFunction(Node) ->
    receive
        Neighbours -> tarry_depth(Node, Neighbours, [])
    end.

%% Tarry's Algorithm with depth-first version
%% Doesn't visit the nodes which have been visited
tarry_depth(Name, Neighbours, OldParent) ->
    receive
        {Sender, Visited} ->
            Parent = case OldParent of [] -> [Sender]; _ -> OldParent end,
            Unvisited = lists:subtract(Neighbours, Visited),
            Next = case Unvisited of
                       [] -> hd(Parent);
                       _ -> lists:nth(rand:uniform(length(Unvisited)), Unvisited)
                   end,
            Self = {Name, self()},
            element(2, Next) ! {Self, [Self | Visited]},
            tarry_depth(Name, Neighbours, Parent)
    end.

2 个答案:

答案 0 :(得分:1)

undef错误意味着程序试图调用未定义的函数。发生这种情况的原因有三个:

  • 没有具有该名称的模块(在这种情况下为rand),或者由于某种原因无法找到并加载该模块
  • 模块未使用该名称和名称定义函数。在这种情况下,所讨论的函数是带有一个参数的uniform。 (请注意,在Erlang中,具有相同名称但参数数量不同的函数被视为单独的函数。)
  • 有这样的功能,但是没有导出。

您可以通过在Erlang shell中键入l(rand).来检查第一个,而通过运行rand:module_info(exports).来检查第二和第三个。

在这种情况下,我怀疑问题是您使用的是旧版的Erlang / OTP。如the documentation中所述,rand模块是在18.0版中引入的。

答案 1 :(得分:0)

如果您提供用于将来的问题的Erlang / OTP版本,那会很好,因为这些年来Erlang发生了很大变化。据我所知,至少在最近的Erlang版本中没有rand:uniform带有arity 2,这就是您遇到的undef错误,在这种情况下,您可以使用crypto:rand_uniform/2像{ {1}}希望对您有所帮助:)