Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/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 'a tree1 = Leaf of 'a |Branch of 'a tree1 * 'a tree1 我想有一个函数,它接受这棵树作为输入,在这棵树中找到一个最小值,将它加倍(让我们把结果称为a),然后复制另一棵相同类型的树,它的所有叶子都被a替换 我有几个功能可以帮助我做到这一点: let rec findmin (mytree: int tree1) : int = match mytree with |Leaf a -

我有一个归纳型树,定义如下:

type 'a tree1 = Leaf of 'a
           |Branch of 'a tree1 * 'a tree1 
我想有一个函数,它接受这棵树作为输入,在这棵树中找到一个最小值,将它加倍(让我们把结果称为a),然后复制另一棵相同类型的树,它的所有叶子都被a替换

我有几个功能可以帮助我做到这一点:

let rec findmin (mytree: int tree1) : int  = match mytree with
|Leaf a -> a
|Branch(Leaf x, Leaf y) -> min x y
|Branch(left, right) -> min (findmin left) (findmin right)  

let rec repdoublemin (mytree: int tree1) : int tree1 = match mytree with
|Leaf a -> Leaf (2*a)
|Branch(Leaf x, Leaf y) -> let result = 2 * findmin (Branch(Leaf x, Leaf y)) 
 in Branch (Leaf result, Leaf result)
|Branch(left,right) -> Branch(repdoublemin left, repdoublemin right)
我的结果不是我所期望的

repdoublemin Branch (Leaf 5, Branch (Leaf 3, Leaf 10));;
- : int tree1 = Branch (Leaf 10, Branch (Leaf 6, Leaf 6))                              
我应该得到:

- : int tree1 = Branch (Leaf 6, Branch (Leaf 6, Leaf 6))                              

我的建议是编写一个helper函数

replace_all_values : 'a -> 'b tree1 -> 'a tree1

@Lhooq有什么想法吗?错误消息有什么不清楚的地方吗?@melpomene我没有要求澄清错误消息。我问了一些修正算法的建议。我可以问一下为什么我在我的帖子上投了1票反对票吗?我知道了。谢谢!你认为我的findmin函数可以找到输入树的最小值吗?我应该在叶a之后添加另一个case吗?像Branch(leaf x,leaf y)->min x y?既然
findmin
工作得很好,请继续melpomene的建议。