F# で trie データ構造を実装しようとしています。私はいくつかの問題を抱えています。単語挿入機能をデバッグできません。この関数内のブレークポイントには到達せず、何かがクラッシュしますが、エラーは表示されません。また、私が正しいことを実装したかどうか、私は深刻な疑問を抱いています。とにかくここにコードがあります:
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# に実装する際に、他にアドバイスがあれば歓迎します。私はこの言語に非常に慣れていないので、多くのことを間違っているに違いないことはわかっています。たとえば、リストが空であるかどうかをチェックしないため、単語挿入機能に欠陥があり、途中で終了することを知っています。その橋に着いたら、私はその橋を渡りたかった。
前もって感謝します
前もって感謝します