繁体   English   中英

迷彩树功能

[英]Ocaml Tree function

我有一棵包含两个元素的树。 由以下数据结构定义:

type ('a,' b) tree =
      empty
    | node of 'a * (' a, 'b) tree sheet;;
    | node of 'b * (' a, 'b) tree sheet;;

现在,我必须编写一个函数split:('a,'b)tree->'a sheet *'b列表,该列表将覆盖第一个列表中的所有元素节点a和第二个列表中的所有元素节点b。

您的代码不正确,因此我假设您的树类型为:

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 [] [];;

split_ func仅在此处具有可读性和便利性。 没有它,您每次必须调用两个空列表。

暂无
暂无

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

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