简体   繁体   English

OCaml:自适应递归函数

[英]OCaml: Adapting recursive function

i've lost all my day in this problem. 我已经整天都在这个问题上了。 So, i have a recursive function like that: 所以,我有一个像这样的递归函数:

let rec rewriteTree tree =
      rewriteElement tree c;
      List.iter rewriteTree tree.subtree in
    rewriteTree myTree

The function rewriteElement returns unit (), so i haven't problems with the function rewriteTree . 函数rewriteElement返回unit(),因此我对函数rewriteTree没有问题。 But, i made changes in my code and the function rewriteElement now returns a boolean, and i need that it returns a boolean list, with all booleans of all nodes of the tree. 但是,我更改了代码,函数rewriteElement现在返回一个布尔值,我需要它返回一个布尔值列表,其中包含树中所有节点的所有布尔值。 What's the best way to do that? 最好的方法是什么? I've tried with List.map, but the compiler throws that the function returns (boolean list list). 我尝试使用List.map,但是编译器抛出该函数返回(布尔列表列表)的情况。 Thanks. 谢谢。

Try this: 尝试这个:

let rec rewriteTree tree =
  rewriteElement tree c ::
    List.concat (List.map rewriteTree tree.subtree) in
rewriteTree myTree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM