Ocaml 如何为树传递递归函数的值
我有递归函数,当然是树的分支Ocaml 如何为树传递递归函数的值,ocaml,Ocaml,我有递归函数,当然是树的分支 type tree = Node of bool ref * (char * tree) list ref type cours= tree ->char list -> char list* char list * tree let cours t word= let rec cours_tree t word l1 l2=match t,word with | Noeud(r,n),[] -> Noud (r,n), l1 , l2
type tree = Node of bool ref * (char * tree) list ref
type cours= tree ->char list -> char list* char list * tree
let cours t word=
let rec cours_tree t word l1 l2=match t,word with
| Noeud(r,n),[] -> Noud (r,n), l1 , l2
| Noeud (r,n),x::h when (List.mem_assoc x !n) ->
x::l1,cours_tree (List.assoc x !n) h l1 l2
| Noeud (r,{content=(c,b)::n),x::h ->
x::l2,cours_tree (List.assoc x b) h l1 l2
in
cours_tree t word [] []
我希望它能从给定的字符列表中浏览一棵树,并返回到达的子树、字符列表和无法到达的字符列表;
但我有一个错误:
Error: This expression has type char list * 'a
but an expression was expected of type 'a
The type variable 'a occurs inside char list * 'a#
我不知道我的问题出在哪里。有很多问题:
Noud
和Noeud
是未定义的构造函数ref
上进行模式匹配时,出现不匹配的{
match
案例返回tree*list*list
但其他案例返回
返回无限类型ref
模式匹配语法。content
应该是contents
列表中的类型错误。assoc
b
是一个树
,但需要类型为(char*'a)列表
的表达式type tree = Node of bool ref * (char * tree) list ref
type cours = tree -> char list -> char list * char list * tree
let cours t word =
let rec cours_tree t word l1 l2 =
match t, word with
| Node (r, n), [] -> Node (r, n), l1, l2
| Node (r, n), x::h when List.mem_assoc x !n ->
let t', l1', l2' = cours_tree (List.assoc x !n) h l1 l2 in
t', x::l1', l2'
| Node (r, {contents = (c, b)::n}), x::h ->
let t', l1', l2' = cours_tree (List.assoc x n) h l1 l2 in
t', l1', x::l2'
in cours_tree t word [] []
你为什么要取消ivg的编辑?这是什么?如果我把它改成
Noeud
,我没有你的错误,我有一个语法错误:语法错误:模式预期
这里:Noeud(r,{content=(c,b)::n),
。请编辑你的代码,这样人们可以得到你的错误,如果你想要答案的话。