私はOCamlでトライを構築しようとしています:
type ('a, 'b) trie = Nil | Cons of 'a * 'b option * ('a, 'b) trie list;;
(* find place to insert key in a list of tries *)
let rec findInsert key x =
match x with
[] -> Nil
| x::xs -> let Cons(k, _, _) = x in
if key = k then x else findInsert key xs;;
(* inser pair in a trie *)
let rec insert ( key, value ) trie =
match trie with
Nil -> Cons(key, value, [])
| t -> let Cons(k, v, trieList) = t and
subTree = insert (key, value) (findInsert key trieList) and
newSubTree = subTree::trieList in
Cons(k, v, newSubTree);;
しかし、これにより次のエラーが発生します。
val findInsert : 'a -> ('a, 'b) trie list -> ('a, 'b) trie = <fun>
File "trie.ml", line 15, characters 54-62:
Error: Unbound value trieList
編集::Virgileのおかげで、コンパイルするプログラムができました:
(* insert pair in a trie *)
let rec insert ( key, value ) trie =
match trie with
Nil -> Cons(key, value, [])
| t ->
let Cons(k, v, trieList) = t in
let subTree = insert (key, value) (findInsert key trieList) in
Cons(k, v, subTree::trieList);;
しかし、実行しようとすると、次のようになります。
# let t = Cons(3, Some 4, []);;
val t : (int, int) trie = Cons (3, Some 4, [])
# insert (4, Some 5) t;;
Error: This expression has type (int, int) trie/1017
but an expression was expected of type (int, int) trie/1260
それらの数字は何を表していますか?