Tail call optimization can be eliminated using continuation. Here is a sample, where is the key and value, stringand intaccordingly
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)
Deleting copies is a bit more complicated if you want to stick with immutable structures
source
share