I have a type tree which has branches and leaves. I would like to get a list of leaves values. So far I'm only able to count the branches.
My tree:
type 'a tr =
| Leaf of 'a
| Branch of 'a tr * 'a tr
And my code:
let getList (tr:float tr)=
let rec toList tree acc =
match tree with
| Leaf _ -> acc 0
| Branch (tl,tr) -> toList tl (fun vl -> toList tr (fun vr -> acc(vl+vr+1)))
toList tr id
Input:
let test=Branch (Leaf 1.2, Branch(Leaf 1.2, Branch(Branch(Leaf 4.5, Leaf 6.6), Leaf 5.4)))
getList test
As a result, I would like to get a list:
[1.2; 1.2; 4.5; 6.6; 5.4]
I've tried some variations similar to this but with no success.
| Branch (tl,tr) -> toList tl (fun vl -> toList tr (fun vr -> (vl::vr)::acc))
toList tr []
Any help would be appreciated.