简体   繁体   中英

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.

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. Without it, you would have to call with two empty lists each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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