我试图Tree
在代码中返回变量,但似乎 Erlang 没有明确设置返回变量。我想像这样从 shell 运行代码:
3> {Pid,Tree} = gbtree:start().
{<0.41.0>,init}
4>
4> gbtree:add_entry(2,Pid,Tree).
** exception error: no function clause matching
gb_trees:is_defined(2,init) (gb_trees.erl, line 221)
in function gb_trees:enter/3 (gb_trees.erl, line 335)
in call from gbtree:add_entry/3 (gbtree.erl, line 13)
请注意,该错误与Tree
未正确传递到gbtree:add_entry(Key,Val,Tree).
. 但是,以不同的方式执行此操作确实可以在 shell 中使用。函数调用gbtree:start().
应该返回类似{<#.#.#>,{0,nil}}
.
2> Tree = gb_trees:empty().
{0,nil}
3>
3> gbtree:add_entry(1,"something",Tree).
gbt_r lookup 1 "something"
{1,{1,"something",nil,nil}}
以下是测试代码gbtree.erl
-module(gbtree).
-export([start/0, init/0, add_entry/3]).
start() ->
register(?MODULE, Pid = spawn(?MODULE, Tree = init, [])),
{ Pid, Tree}.
init() ->
Tree = gb_trees:empty(),
Tree.
add_entry(Key, Data, Tree1) ->
Tree2 = gb_trees:enter( Key, Data, Tree1),
io:format("gbt_r lookup ~p ~p~n ",[Key, Data]),
Tree2.