0

私はこの破壊的な挿入を試みて作成しました:

(* 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)
4

1 に答える 1

1

エラーメッセージの元Emptyとなるデータ型の定義を提供していただけると助かります。Node

これは、データ型定義が最初の関数用であると想定しているものです。

datatype 'a trie = Empty | Node of 'a * 'a trie ref list

2番目の場合:

datatype 'a trie = Empty | Node of 'a * 'a trie list

2 番目の関数にはいくつかの問題があります。

最初の節 ( )はns ([], t) = t@Emptyに追加しよtEmptyとします。破壊的なバージョンに一致するように変更して、型チェックを行う必要があります。tlistEmpty'a triens ([], t) = t@[Empty]

使用中の句はcase、等号ではなく「太い矢印」を使用しています。これに置き換え| Empty = h::ins(x::xs, tl)ます| Empty => h::ins(x::xs, tl)

最後に、これは有効な SML ではありません。

if c = x then
(h = Node (c, ins (xs, lis)); h::tl)

括弧で囲まれた式は、命令コード専用のシーケンスです。変数hは参照ではないため、そのように割り当てることはできません。代わりに、a を使用しletてローカル変数を導入する必要があります。

if c = x then
       (let val h = Node (c, ins (xs, lis)) in  h::tl end)
else

これが最後の関数です。これはコンパイルされますが、慎重にテストしていないため、別のエラーがあるかどうかはわかりません。

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
                   (let val h = Node (c, ins (xs, lis)) in  h::tl end)
            else
                h::ins (x::xs, tl)
      | Empty => h::ins(x::xs, tl)

    if c = x then
    (h = Node (c, ins (xs, lis)); h::tl)

    if c = x then
           (let val h = Node (c, ins (xs, lis)) in  h::tl end)
    else
于 2013-02-13T17:05:02.003 に答える