假期里,我的家人喜欢玩Boggle。问题是,我的Boggle 技术很糟糕。所以我做了任何优秀程序员都会做的事情:编写一个程序来给我玩。
该算法的核心是一个简单的前缀特里树 http://en.wikipedia.org/wiki/Trie,其中每个节点是一个dict
下一个字母的参考文献。
这是trie:add
执行:
add([], Trie) ->
dict:store(stop, true, Trie);
add([Ch|Rest], Trie) ->
% setdefault(Key, Default, Dict) ->
% case dict:find(Key, Dict) of
% { ok, Val } -> { Dict, Val }
% error -> { dict:new(), Default }
% end.
{ NewTrie, SubTrie } = setdefault(Ch, dict:new(), Trie),
NewSubTrie = add(Rest, SubTrie),
dict:store(Ch, NewSubTrie, NewTrie).
您可以在此处查看其余部分以及如何使用它的示例(在底部):
http://gist.github.com/263513 http://gist.github.com/263513
现在,这是我在 Erlang 中的第一个严肃的程序,我知道它可能有很多问题......但我最关心的是它使用800 MB 内存.
那么,我做的最错的是什么?我怎样才能让它少一点错误呢?