我试图在F#中实现trie数据结构。 我有一些问题。 我无法调试单词插入功能。我的这个函数里没有任何断点出现了崩溃,但是我没有看到任何错误。 另外,如果我正确地实施了这件事,我会有很大的怀疑。 反正这里是代码:新手F#实施错误
type TrieNode =
| SubNodes of char * bool * TrieNode list
| Nil
member this.Char = match this with | Nil -> ' '
| SubNodes(c,weh,subnodes) -> c
member this.GetChild(c:char) = match this with | Nil -> []
| SubNodes(c,weh,subnodes) ->[ (List.filter(fun (this:TrieNode) -> this.Char = c) subnodes).Head ]
member this.AWordEndsHere = match this with | Nil -> false
| SubNodes(c,weh,subnodes) -> weh
module TrieFunctions =
let rec insertWord (wordChars:char list) = function
| Nil -> SubNodes(wordChars.Head, false, [])
| SubNodes(c, weh, subnodes) as node ->
let child = node.GetChild(wordChars.Head)
if child = [] then
SubNodes(wordChars.Head,false,[insertWord wordChars.Tail node])
else
SubNodes(wordChars.Head,false,[insertWord wordChars.Tail child.Head])
type Trie(inner : TrieNode) =
member this.InsertWord(wordChars:char list) = TrieFunctions.insertWord(wordChars)
let trie = Trie(SubNodes(' ',false,List.empty)).InsertWord(['g';'i';'g';'i'])
所以我的问题是:
1.我怎样才能到insertWord功能调试访问?为什么我现在没有得到它?为什么我没有看到错误?
2.如何使函数插入字返回TrieNode对象的列表,以便我不必将方括号(“[”,“]”)括起来。我认为这是一个错误。
3.你可以给我任何其他的建议,在F#中实现这个数据结构是欢迎的我知道我必须做很多错误的事情,因为我对这种语言很陌生。我知道例如单词插入函数有缺陷,因为它不检查列表是否为空,所以它过早地结束。当我遇到它时,我想穿过那座桥。
预先感谢您
预先感谢您
没有确定就不想编辑 - 你的意思是一棵树,对吧? 列表和二叉树的扩展? – 2011-02-15 20:52:57
最后的trie值最终是type(Trie - > Trie) - 这表明它正在部分地评估插入字 – Jimmy 2011-02-15 21:00:17