Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable Trie structure in F#

I am playing around with the aho-corasick algorithm to try and get a little better with F#, and I ran across a problem with the Trie implementations, they are all mutable or can't be tail call optimized.

The basic issue from what I can see is that immutable data structures have to be built "bottom up" since you can't change what they point to, so your options are either make them mutable, or find out the nodes as you go along(i.e. recurse in construction).

Is there any way of making an immutable trie data structure with tail call optimizations on the construction?(and not lose efficiency by copying.)

like image 248
Snark Avatar asked Mar 22 '11 18:03

Snark


2 Answers

The tail call optimiation can be eliminated by using a continuation. Here's a sample where the key and value are string and int respectively

type Trie = 
 | Data of string * int * Trie * Trie 
 | Leaf 

let Insert start key value = 
  let rec inner current withNode = 
    match current with
    | Data (currentKey, currentValue, left, right) ->
      if key < currentKey then
        inner left (fun left -> Data (currentKey, currentValue, left, right))
      else 
        inner right (fun right -> Data (currentKey, currentValue, left, right))
    | Leaf -> withNode (Data (key, value, Leaf, Leaf))
  inner start (fun x -> x)

Eliminating the copies is a bit more difficult if you want to stick with immutable structures

like image 152
JaredPar Avatar answered Sep 18 '22 10:09

JaredPar


I came across this post while researching for my code review post where I've implemented at immutable trie.

It is performant using maps for links rather than binary trees.

like image 21
psaxton Avatar answered Sep 18 '22 10:09

psaxton