I am trying to write to recursive fn that changes a unlabelled tree to a labelled tree with the use of a helper function.
required info:
type nucleotide =
| G | C
| A | T
type helix = nucleotide list
type tree =
| Leaf of helix
| Node of tree * tree
type labeled_tree =
| LLeaf of helix
| LNode of labeled_tree * helix * labeled_tree
This is the helper function:
let rec guess_parent_helix (x1: helix) (x2: helix) : helix =
begin match (x1, x2) with
([], []) -> []
| (h1 :: t1, h2 :: t2) ->
(if h1 = h2 then h1 else A) :: guess_parent_helix t1 t2
| _ -> failwith "invalid input: x1 and x2 have unequal lengths"
end
I have written the following function but am unable to compile as it says I have not exhausted the pattern matching : Node( (Node(_ , _ ),_) ). What I do not understand is, how is this not included in the Node(rt,lt) pattern match:
let rec add_ancestor_labels (r: tree) : labeled_tree =
begin match r with
| Leaf x -> LLeaf x
| Node (lt,rt) ->
begin match r with
| Leaf x -> LLeaf x
| Node (Leaf[m], Leaf[n]) ->
LNode (add_ancestor_labels lt, guess_parent_helix [m] [n], add_ancestor_labels rt)
end
end