Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ocaml 如何为树传递递归函数的值_Ocaml - Fatal编程技术网

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),
    。请编辑你的代码,这样人们可以得到你的错误,如果你想要答案的话。