【发布时间】:2018-09-24 00:09:48
【问题描述】:
我正在尝试在 OCaml 中构建一个初始的 Trie 结构,其中边缘是字符。所以字符串“ESK”将被映射为:
[('E', [('S', [('K', [])])])]
我对此的定义是:
type trie = Trie of (char * trie) list
但是,在实现 add 函数时:
let rec add_string str =
let key = String.get str 0 in
if String.length str = 1 then
(key, empty_trie) :: []
else
(key, add_string (tail str)) :: []
对于add (tail str),编译器给了我:
Error: This expression has type (char * trie) list
but an expression was expected of type trie
我对此有点困惑,因为我没有将trie 定义为(char * trie) list?
tail 就是let tail str = String.slice str 1 (String.length str) 而empty_trie 就是let empty_trie = Trie([])
【问题讨论】:
标签: functional-programming ocaml variant