我创建了这个破坏性插入尝试:
(* ins: char list * (char trie) ref list -> (char trie) ref list *)
fun ins ([], t) = t@[ref Empty]
| ins (x::xs, []) = [ref (Node (x, ins (xs, [])))]
| ins (x::xs, h::tl) =
case !h of
Node (c, lis) =>
if c = x then
(h := Node (c, ins (xs, lis)); h::tl)
else
h::ins (x::xs, tl)
| Empty => h::ins(x::xs, tl)
我试图让它在没有引用的情况下正常插入,但我不断收到错误。
(* ins: char list * (char trie) list -> (char trie) list *)
fun ins ([], t) = t@Empty
| ins (x::xs, []) = (Node (x, ins (xs, [])))
| ins (x::xs, h::tl) =
case h of
Node (c, lis) =>
if c = x then
(h = Node (c, ins (xs, lis)); h::tl)
else
h::ins (x::xs, tl)
| Empty = h::ins(x::xs, tl)