2013-06-19 52 views
0

我有一棵树,它包含两个元素。Ocaml树函数

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

现在我必须写一个函数split: - >“的片材*” B列表,这将覆盖所有元素节点的在(“A,” B)树是由以下数据结构定义第一个列表和第二个列表中的所有元素节点b。

+1

您的代码在语法上不正确,因为它们是两个';;'。我不确定是否有两个构造函数,第三个应该被忽略,或者有三个,第二个和第三个具有相同的名称是错误的。你能修复你的代码吗? – gasche

回答

1

你的代码是不正确的,所以我认为你的树类型是:

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只是这里的可读性和方便性。没有它,你将不得不每次打两个空列表。