[英]Ocaml Tree function
I have a tree that contains two elements. 我有一棵包含两个元素的树。 Is defined by the following data structure:
由以下数据结构定义:
type ('a,' b) tree =
empty
| node of 'a * (' a, 'b) tree sheet;;
| node of 'b * (' a, 'b) tree sheet;;
Now i have to write a function split: ('a,' b) tree -> 'a sheet *' b list, which overwrites all elements node a in the first list and all elements node b in the second list. 现在,我必须编写一个函数split:('a,'b)tree->'a sheet *'b列表,该列表将覆盖第一个列表中的所有元素节点a和第二个列表中的所有元素节点b。
Your code is not correct, so I assumed that your tree type was : 您的代码不正确,因此我假设您的树类型为:
type ('a, 'b) tree =
| Empty
| AlphaNode of 'a * ('a, 'b) tree
| BetaNode of 'b * ('a, 'b) tree
let split tree =
let rec split_ tree a b = match tree with
| Empty -> (a, b)
| AlphaNode (sheet, tree) -> split_ tree (sheet::a) b
| BetaNode (sheet, tree) -> split_ tree a (sheet::b) in
split_ tree [] [];;
The split_ func is only here for readability and convenience. split_ func仅在此处具有可读性和便利性。 Without it, you would have to call with two empty lists each time.
没有它,您每次必须调用两个空列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.