Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:50:05+00:00 2026-05-20T04:50:05+00:00

I am trying to implement a trie data structure in F#. I am having

  • 0

I am trying to implement a trie data structure in F#.
I am having some problems.
I cannot debug the word insertion function. None of my breakpoint inside this function is reached something crashes but I don’t see any error.
Also I am having serious doubts if I implemented the thing right.
Anyway here is the code:

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'])

So my questions are:
1. how can I get debug access to the insertWord function? Why am I not getting it now? Why am I not seeing an error?
2. How can I make the function insert word return a list of TrieNode objects so that I won’t have to wrap the call around square brackets (“[“,”]”). I think this is an error.
3. Any other advice you can give me on implementing this data structure in F# is welcomed I know I must be doing a lot of things wrong as I am very new to this language. I know for example that the word insertion function is flawed because it doesn’t check if the list is empty or not so it ends prematurely. I wanted to cross that bridge when I got to it.

Thank you in advance

Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:50:06+00:00Added an answer on May 20, 2026 at 4:50 am
    1. You are probably failing to trigger your breakpoint because you aren’t fully applying insertWords: it takes two curried parameters, but you’re only passing the single argument wordChars in. Perhaps you meant to define your Trie type like this instead?

      type Trie(inner : TrieNode) =
        member this.InsertWord(wordChars:char list) = TrieFunctions.insertWord wordChars inner
      
    2. Well, you could wrap all of your return values in [] to make them singleton lists and then not wrap the recursive calls to insertWords. However, it seems likely that there’s something wrong with your algorithm (either way), since you only ever get singleton lists…

      Note that at the moment you’re completely discarding the existing subnodes list – if you want to append to the front of it, use (insertWord wordChards.Tail node)::subnodes instead. However, sometimes you’ll want to replace an existing entry rather than append a new one, which will require more effort.

    3. There are several issues. Here are a few to get you started:

      • Try to avoid using Head, particularly since you don’t always know that your lists are non-empty when you’re calling it.
      • When you’re inserting a word into an empty Trie, you’re dropping all but the first character! Similarly, you need to reconsider your recursive calls too.
      • More importantly, your TrieNode type has a bit of a problem. Can you write out the resulting trie you would expect to see which just contains the two words "in" and "to"?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement AVAudioplayer and get some metering data of the played music, but
I'm trying to implement a data compression idea I've had, and since I'm imagining
While trying to learn c++, I tried to implement class representing very basic trie.
I am trying implement the Data transformation using Reflection 1 example in my code.
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
Ok well I'm trying implement something similar to the 'undo' function in many image
I am trying to implement this function which works everywhere except every IE version
Im trying to implement some code i found on a website which duplicates a
Just trying to implement mobFox into my app, but having trouble to make it
I am trying to implement functionality wherein an attribute, once set, cannot be changed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.