我有一个归纳类型树,定义如下:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-lg-4 col-md-6 boxShadow">
<div class="card card-pricing card-plain">
<h6 class="card-category"> Bravo Pack</h6>
<div class="card-body">
<div class="card-icon icon-warning ">
<i class="now-ui-icons media-1_button-power"></i>
</div>
<h3 class="card-title" style="color: #333;">10$</h3>
<ul>
<li style="color: #888;">Complete documentation</li>
<li style="color: #888;">Working materials in Sketch</li>
</ul>
</div>
</div>
</div>
<div class="col-lg-4 col-md-6 boxShadow">
<div class="card card-pricing">
<h6 class="card-category"> Alpha Pack</h6>
<div class="card-body">
<div class="card-icon icon-primary ">
<i class="now-ui-icons objects_diamond"></i>
</div>
<h3 class="card-title">69$</h3>
<ul>
<li>Working materials in EPS</li>
<li>6 months access to the library</li>
</ul>
</div>
</div>
</div>
<div class="col-lg-4 col-md-6 boxShadow">
<div class="card card-pricing card-plain">
<h6 class="card-category"> Charlie Pack</h6>
<div class="card-body">
<div class="card-icon icon-success ">
<i class="now-ui-icons media-2_sound-wave"></i>
</div>
<h3 class="card-title" style="color: #333;">69$</h3>
<ul>
<li style="color: #888;">Working materials in PSD</li>
<li style="color: #888;">1 year access to the library</li>
</ul>
</div>
</div>
</div>
</div>
我希望有一个函数将这个树作为输入,在这个树中找到一个min值,将它加倍(让我们将结果称为a),然后再生另一个相同类型的树,它的所有叶子都被a。替换。
我有几个功能可以帮助我做到这一点:
type 'a tree1 = Leaf of 'a
|Branch of 'a tree1 * 'a tree1
我的结果不是我的预期。
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))
答案 0 :(得分:2)
我的建议是写一个辅助函数
replace_all_values : 'a -> 'b tree1 -> 'a tree1