user2258100
user2258100

Reputation: 9

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.

Upvotes: 0

Views: 197

Answers (1)

ChristopheLec
ChristopheLec

Reputation: 856

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.

Upvotes: 1

Related Questions